[英]Change link attributes within a class using jquery
Without manually assigning an ID to each href, the goal would be to change the URLs for both hrefs to a different URL 无需手动为每个href分配ID,目标是将两个href的URL更改为不同的URL
<div class="example">
<p><a href="http://www.google.com/example?test1332">Test Site</a></p>
<p><a href="http://www.google.com/example?test1332">Test Site</a></p>
</div>
This is my jquery attempt which is not working 这是我的jQuery尝试不起作用
$(".example").each(function() {
this.setAttribute("href", this.getAttribute("href").replace("http://www.test.com"));
});
This is my fiddle http://jsfiddle.net/n322j/ 这是我的小提琴http://jsfiddle.net/n322j/
You seem to want 你好像要
$('.example a').attr('href', "http://www.test.com");
If you'd want to only replace part of the URL, that is keep everything apart http://www.google.com
, then you could do 如果您只想替换一部分网址,那就是将所有内容分开http://www.google.com
,那么您可以这样做
$(".example a").attr('href', function(i, href) {
return href.replace("http://www.google.com", "http://www.test.com");
});
another way of doing this. 另一种方式。
<script>
$("document").ready(function (){
$("div p a").attr('href',"http://www.google.com");
});
</script>
it also works fine. 它也可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.