开发者

updating selected href value throught the page using jquery

update all the href value in a page using jquery. I have the href="http://www.google.com" coming through out the page and i wanted to u开发者_运维百科pdate the href mentioning above to be changed to "http://www.test.com" how i can get this done.


$('[href]').each(function () {
  $(this).attr('href', 'http://www.test.com');
});


$('a[href*="google"]').attr('href', 'http://www.test.com');

The selector will go through all links that have google somewhere in their href attribute with *= and if so, it will update their attribute accordingly.


use selectors

<script type="text/javascript">
  $("a[href*='http://www.google.com']").attr('href','http://www.test.com');
</script>


Save this as an .html file for a complete working example!

<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  <script>
    $(document).ready(function(){
        $("a[href*='http://www.google.com']").attr('href','http://www.test.com').html('Test.com');
    });
  </script>
</head>
<body>
     <a href="http://www.google.com">Google</a>
     <a href="http://www.google.com">Google</a>
     <a href="http://www.google.com">Google</a>
     <a href="http://www.NotGoogle.com">Not Google</a>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜