开发者

jquery sender BY CLASS

<img href="a" class="myImg"></img>
<img href="b" class="myImg"></img>
<img href="c" class开发者_如何学运维="myImg"></img>

How can I determine the href value of the image being clicked maybe by tracking click events on elements using css class myImg. You can also modify the html if it simplifies the jquery.Thanks


$('img.myImg').click(function(){
    alert(this.href); //might not work
    alert(this.getAttribute('href')); //definitely should work
});


$('.myImg').click(function() {
   alert($(this).attr('href'));
});


Try this

$('.myImg').click(function(){
    alert($(this).attr('href'));
});


$(document).ready(function() {
  $(".myImg").click(function(e) {
    alert($(this).attr("href"));
  });
});


"img" tag doesn't have a "href" attribute. You have to put those images between link tags ("a");

<a href="a"><img src=".." class="myImg"></a>
<a href="b"><img src=".." class="myImg"></a>
<a href="c"><img src=".." class="myImg"></a>

Then if you want to get the href, listen for the click event on the link;

$('a').click(function(e) {
    console.log('selected href:', $(this).attr('href'));
    // if you want you can stop executing the href
    // e.preventDefault();
});

Check http://jsfiddle.net/demods/U7D2g/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜