在jQuery中通过alt标记定位图像以禁用超链接

时间:2011-11-11 13:53:35

标签: jquery

我正在尝试通过它的alt标记来定位图像,以关闭自动生成的超链接,该超链接在服务器端执行,我无法编辑。

我正在使用的代码是:

$('.ssImage a[title="test image"]').click(function(){return false;});

哪个不起作用....

HTML:

<div class="ssImage"><a href="/content/test.htm" title="test image"><img src="/content/test.jpg" alt="test image"/></a></div>

我无法定位div本身,因为它会影响页面上具有相同类的其他图像,因此我需要找到一种通过alt或title指定图像的方法。

感谢任何可以提供帮助的人!

2 个答案:

答案 0 :(得分:3)

我认为选择器工作正常,而不是阻止点击的return false;。试试这个:

$('.ssImage a[title="test image"]').click(function(e){ 
    e.preventDefault(); 
});

答案 1 :(得分:0)

这应该这样做:

工作小提琴:http://jsfiddle.net/mnLr8/

$('.ssImage a[title="test image"]').click(function(e){
    e.preventDefault();
    alert('clicked the link');
});

将事件传递给e,然后是e.preventDefault();停止点击被激活。