[英]Find http protocol in some links and convert it to https
On my little code, I'm loading a script that changes the colors of links but I also want to replace the http
where needed with https
using some Regex but I'm facing an error with replace
function in JS. On my little code, I'm loading a script that changes the colors of links but I also want to replace the
http
where needed with https
using some Regex but I'm facing an error with replace
function in JS.
The Regex for JS would only replace to https://
when a string starts with http://
(case-insensitive), otherwise the original string is returned as is.当字符串以
http://
(不区分大小写)开头时,JS 的正则表达式只会替换为https://
,否则将按原样返回原始字符串。
The code so far looks like in the snippet, the colors are changing for all the links but replace
has issues.到目前为止的代码看起来像在片段中,colors 正在更改所有链接,但
replace
有问题。
Please let me know what's wrong with the code so far.请让我知道到目前为止的代码有什么问题。
window.onload = function() { // alert('Page loaded'); let url = document.querySelectorAll('.page-numbers'); console.log(url); url.forEach((e) => { e.style.color = 'red'; console.log(e); e.replace(/^http:\/\//i, 'https://'); }); };
<div class="pagingSection"> <a href="http://www.example.com" class="page-numbers">Link 1</a> <a href="https://www.example.com" class="page-numbers">Link 2</a> <a href="http://www.example.com" class="page-numbers">Link 3</a> <a href="//www.example.com" class="page-numbers">Link 4</a> <a href="" class="page-numbers">Link 5</a> </div>
You have to edit the href
property of the link:您必须编辑链接的
href
属性:
Take a look at this snippet.看看这个片段。 I only modified the
e.replace
line.我只修改了
e.replace
行。
window.onload = function() { // alert('Page loaded'); let url = document.querySelectorAll('.page-numbers'); console.log(url); url.forEach((e) => { e.style.color = 'red'; console.log(e); e.href = e.href.replace(/^http:\/\//i, 'https://'); }); };
<div class="pagingSection"> <a href="http://www.example.com" class="page-numbers">Link 1</a> <a href="https://www.example.com" class="page-numbers">Link 2</a> <a href="http://www.example.com" class="page-numbers">Link 3</a> <a href="//www.example.com" class="page-numbers">Link 4</a> <a href="" class="page-numbers">Link 5</a> </div>
In JS if you have to edit an attribute of an HTML element you should use setAttribute
.在 JS 中,如果您必须编辑 HTML 元素的属性,您应该使用
setAttribute
。
Also you are getting an error because the url
variable is not an array of string, it's an array HTML element so it dont have a replace function, you should use e.href
to read the url Also you are getting an error because the
url
variable is not an array of string, it's an array HTML element so it dont have a replace function, you should use e.href
to read the url
window.onload = function() { // alert('Page loaded'); let url = document.querySelectorAll('.page-numbers'); url.forEach((e) => { e.style.color = 'red'; e.setAttribute('href', e.href.replace(/^http:\/\//i, 'https://')); console.log(e); }); };
<div class="pagingSection"> <a href="http://www.example.com" class="page-numbers">Link 1</a> <a href="https://www.example.com" class="page-numbers">Link 2</a> <a href="http://www.example.com" class="page-numbers">Link 3</a> <a href="//www.example.com" class="page-numbers">Link 4</a> <a href="" class="page-numbers">Link 5</a> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.