使用Jquery,我怎么说(这是在click()函数中):
if(element clicked has an href == 'the href location'){
};
我使用的是这样的东西:
if ($(this).attr('href') == 'image.png') {
//Do Stuff
};
这不起作用,因为我发现$(this)指的是窗口。此外,我不知道如何使用$(this)特定的东西。
任何帮助将不胜感激。
答案 0 :(得分:1)
如果您只想在符合此条件的元素上触发单击事件,请使用:
$('a[href="image.png"]').click(function(e)
{
});
如果要在选择器的所有元素上触发单击事件,但是如果它们具有特定的href,则执行一些特殊操作,您可以正确但 $(this)
仅指向点击事件回调中的元素。
$('a').click(function(e)
{
if ($(this).attr('href') == 'image.png') { ... }; // it works within this function
});
答案 1 :(得分:0)
你在寻找这样的东西吗?
$(document).ready(function() {
$('img').click(function() {
var target = $(this).attr('src');
if( target === 'http://www.mallorcaweb.net/masm/Planetas/Jupiter.jpg' ) {
alert('you have found it!');
}else{
alert('wrong one..');
}
});
});