在 href 中查找包含特定字符串的链接,并仅删除带有 javascript 的斜杠之间的 href
Fnd links containing particular string in href and remove href between slashes with javascript only
我有一个用例,我必须 select 所有 <a>
,在 url 中包含字符串,例如“/web/local”,并删除“[=18=” ]" 来自所有这些链接的所有 href。
注意:我无法使用 jQuery。我可以使用纯 js 或 YUI。
提前致谢。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<a href="http:///web/locale/google.com">Link 1</a>
<a href="http:///web/locale/whosebug.com">Link 2</a>
<script>
var string = '/web/locale/';
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var link = links[i].getAttribute('href');
link = link.replace(string, '');
links[i].setAttribute('href', link);
}
</script>
</body>
</html>
查看内联评论:
let phrase = "/web/local";
// Get all the links that contain the desired phrase into an Array
let links = Array.prototype.slice.call(document.querySelectorAll("a[href*='" + phrase +"']"));
// Loop over results
links.forEach(function(link){
// Remove the phrase from the href
link.href = link.href.replace(phrase, "");
});
// Just for testing:
console.log(document.querySelectorAll("a"));
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
为了正确获取/设置 href 属性,您需要使用 getAttribute/setAttribute:
document.querySelectorAll('a[href*="/web/local"').forEach(function(ele) {
ele.setAttribute('href',
ele.getAttribute('href').replace('/web/local', ''));
console.log(ele.outerHTML);
});
<a href="/web/local"></a>
<a href="22222/web/local"></a>
<a href="/web/local"></a>
我有一个用例,我必须 select 所有 <a>
,在 url 中包含字符串,例如“/web/local”,并删除“[=18=” ]" 来自所有这些链接的所有 href。
注意:我无法使用 jQuery。我可以使用纯 js 或 YUI。
提前致谢。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<a href="http:///web/locale/google.com">Link 1</a>
<a href="http:///web/locale/whosebug.com">Link 2</a>
<script>
var string = '/web/locale/';
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var link = links[i].getAttribute('href');
link = link.replace(string, '');
links[i].setAttribute('href', link);
}
</script>
</body>
</html>
查看内联评论:
let phrase = "/web/local";
// Get all the links that contain the desired phrase into an Array
let links = Array.prototype.slice.call(document.querySelectorAll("a[href*='" + phrase +"']"));
// Loop over results
links.forEach(function(link){
// Remove the phrase from the href
link.href = link.href.replace(phrase, "");
});
// Just for testing:
console.log(document.querySelectorAll("a"));
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
为了正确获取/设置 href 属性,您需要使用 getAttribute/setAttribute:
document.querySelectorAll('a[href*="/web/local"').forEach(function(ele) {
ele.setAttribute('href',
ele.getAttribute('href').replace('/web/local', ''));
console.log(ele.outerHTML);
});
<a href="/web/local"></a>
<a href="22222/web/local"></a>
<a href="/web/local"></a>