最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览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)');

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)');
Share Improve this question edited Oct 8, 2012 at 16:21 caitriona 9,1614 gold badges33 silver badges36 bronze badges asked Dec 16, 2010 at 10:34 TomkayTomkay 5,16021 gold badges65 silver badges94 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布评论

评论列表(0)

  1. 暂无评论