函数 $(this).attr('src');在 img 上未定义
function with $(this).attr('src'); undefined on img
我需要帮助来理解 $(this) 的确切工作原理,因为我无法使这个简单的代码工作:
HTML:
<img style="cursor:pointer;" onclick="openfullsize();" src="/customimage/test.png"/>
JS:
function openfullsize(){
var path = $(this).attr("src");
alert(path);
}
我想知道我是否做错了什么,或者我只是不明白 $(this) 的行为方式。 $(this) 应该引用调用它的元素,对吗?在这种情况下,它将是我的 img。
这应该提醒 src 属性的内容,但它是未定义的。
我希望这个在 Jquery 中。
感谢您的帮助,这是 JsFiddle
您应该直接在 JS 代码中绑定事件处理程序,而不是像您现在所做的那样在 HTML 中绑定。
$("img").on("click", function(){
//$(this) will now work as expected
});
我需要帮助来理解 $(this) 的确切工作原理,因为我无法使这个简单的代码工作:
HTML:
<img style="cursor:pointer;" onclick="openfullsize();" src="/customimage/test.png"/>
JS:
function openfullsize(){
var path = $(this).attr("src");
alert(path);
}
我想知道我是否做错了什么,或者我只是不明白 $(this) 的行为方式。 $(this) 应该引用调用它的元素,对吗?在这种情况下,它将是我的 img。
这应该提醒 src 属性的内容,但它是未定义的。
我希望这个在 Jquery 中。
感谢您的帮助,这是 JsFiddle
您应该直接在 JS 代码中绑定事件处理程序,而不是像您现在所做的那样在 HTML 中绑定。
$("img").on("click", function(){
//$(this) will now work as expected
});