简体   繁体   中英

JQuery - Change the href of the li a 's to javascript:void(0)

I have a thumbnail list:

<ul>
  <li><a href=#c1><img /><p>1 Thumb</p></a></li>
  <li><a href=#c2><img /><p>2. Thumb</p></a></li>
  <li><a href=#c3><img /><p>3. Thumb</p></a></li>
</ul>

And javascript (i use jquery framework) should change every href of the a 's in the ul to javascript:void(0)

It's like:

$("#thumbs ul li > a").href( 'javascript:void(0)');

You can do what you want using .attr() (used for attributes, instead of .href() ) like this:

$("#thumbs ul li > a").attr('href','javascript:void(0)');

...but I wouldn't, there's a better way to solve your problem, for example:

$("#thumbs ul li > a").click(function(e) {
  e.preventDefault();
});

This attaches a property click handler to prevent the navigation, rather than messing with attributes to do the same.

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