admin管理员组文章数量:1427780
I'm having difficulty understanding how to use event.data to return the value of whatever element the event fired one. Here my example:
<a href="#" class="dw_add">
<img src="http://something" />
</a>
var callback = function(event){ console.log(event.data.src) }
$('.dw_add')
.on('click', { src: $(this).find('img').attr('src') },callback);
$this is currently referencing the document and not the that was clicked. Hopefully you can see what I'm trying to do, but my javascript knowledge is limited, think I'm approaching it in a PHP mindset.
Any ideas how to pass the value of 'img src' to the callback?
I'm having difficulty understanding how to use event.data to return the value of whatever element the event fired one. Here my example:
<a href="#" class="dw_add">
<img src="http://something" />
</a>
var callback = function(event){ console.log(event.data.src) }
$('.dw_add')
.on('click', { src: $(this).find('img').attr('src') },callback);
$this is currently referencing the document and not the that was clicked. Hopefully you can see what I'm trying to do, but my javascript knowledge is limited, think I'm approaching it in a PHP mindset.
Any ideas how to pass the value of 'img src' to the callback?
Share Improve this question edited Dec 22, 2015 at 20:22 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 26, 2012 at 23:50 lowe_22lowe_22 3951 gold badge6 silver badges14 bronze badges3 Answers
Reset to default 3You are not inside a functions scope, so this:
$('.dw_add').on('click', { src: $(this).find('img').attr('src') },callback);
will not work as $(this) is not the element you think it is because there is no local scope for the clicked element, no event.target or event.data etc!
However this will work as it has a direct reference to the element and scope for "this" is not an issue:
var callback = function(event){ console.log(event.data.src) }
var elm = $('.dw_add');
elm.on('click', { src: elm.find('img').attr('src') },callback);
and this will work as $(this) is inside a function scope:
function callback(a){ console.log(a) }
$('.dw_add').on('click', function() {
var a = $(this).find('img').attr('src');
callback(a);
});
Also, using $(this) inside the callback function will work, as it is, again, inside a functions scope and has a reference to the targeted element.
Why not do something like this:
var callback = function(event){ console.log($(this).find('img').attr('src')) }
$('.dw_add')
.on('click', callback);
In jQuery event handlers, this
is set to the DOM element that fired the event. Since you're attaching an event handler to the <a>
element, this
will be the link.
If you want to get the src
of the img
, you'll have to find the img
element in the triggering a
by using find()
or children()
:
$('.dw_add').on('click', function(e) {
$(this).children('img').attr('src');
});
本文标签: javascriptPassing Event data to callback function with jQueryStack Overflow
版权声明:本文标题:javascript - Passing Event data to callback function with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745503688a2661148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论