简体   繁体   中英

jQuery to dynamically add source to A tag

I have a parent div tag like this

<div class="top-news-heading">Top News Items<br>
<ul>
   <li>
      <div id="xlaANMzone_4f503f">
       <div id="4f503f">
          <div align="left" style="width: 100%; padding: 0px; margin: 0px;">
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1231&z=44"> News 1</a><br>
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1232&z=44"> News 2</a><br>
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1233&z=44"> News 2</a><br>

        </div>
      </div>
     </div>
   </li>
   <li>
      <div id="xlaANMzone_4f503f">
       <div id="4f503f">
          <div align="left" style="width: 100%; padding: 0px; margin: 0px;">
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1236&z=44"> News 4</a><br>
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1238&z=44"> News 5</a><br>
              <a href="http://www.onesite.com.au/news_manager/templates/?a=1230&z=44"> News 6</a><br>

        </div>
      </div>
     </div>
   </li>
</ul>

</div> <!--parent div ends-->

I want to change href of every child tag to http://www.offsite.com/article.asp/a=1238&z=44 . using jQuery or JavaScript....

Note that query strings "a" and "z" should be the original ones. Any help will be appreciated.

 $(document).ready(function(){
      $(".top-news-heading a").attr("href","your_new_link");
    });

If you just want to replace the string like I guess do

$('a').each(function(){
    var $this = $(this);
    $this.attr('href', $this.attr('href').replace(/onesite/, 'offsite'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM