[英]Change hyperlink to something else with js or jquery
What is the best way to change a link like etc this: 改变像这样的链接的最好方法是什么:
<a href="replacethislink">button</a>
to 至
<a href="#" class="replaced">button</a>
With either js or jQuery? 使用js还是jQuery?
You can use, 您可以使用,
attr()
for updating href
attribute attr()
用于更新href
属性 addClass()
for adding the class addClass()
用于添加类 $('a[href="replacethislink"]').attr('href', '#').addClass('replaced');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <a href="replacethislink">button</a>
Using javascript (modern browser) : 使用javascript(现代浏览器):
var linkToReplace = document.querySelectorAll('a[href="replacethislink"]');
linkToReplace.setAttribute("href", "#");
linkToReplace.setAttribute("class", "replaced");
Using javascript (older browser) : 使用javascript(旧版浏览器):
var linkToReplace = null;
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
if (links[i].getAttribute("href") == "replacethislink") {
linkToReplace = links[i];
}
}
if(linkToReplace) {
linkToReplace.setAttribute("href", "#");
linkToReplace.setAttribute("class", "replaced");
}
Using jQuery : 使用jQuery:
var linkToReplace = $('a[href="replacethislink"]');
linkToReplace.attr("href", "#");
linkToReplace.addClass("replaced");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.