admin管理员组文章数量:1363794
I've a box from divs (price_item) with several child divs (date, price etc.). I'm using it as a caroussel for showing prices. If one clicks on any child, the parent's background color must be changed to red. I wrote it like:
$(...selectquery...).click(function() {
var $tgt = $(event.target);
$tgt.parent().css("backgroundColor", "red");
});
on Chrome, event.target is working as expected, on Firefox 3.0.1 it says "event is not defined". How can I work around this?
I've tried the following:
$(this).parent().css()
it says 'object is not a function'
thanks!
I've a box from divs (price_item) with several child divs (date, price etc.). I'm using it as a caroussel for showing prices. If one clicks on any child, the parent's background color must be changed to red. I wrote it like:
$(...selectquery...).click(function() {
var $tgt = $(event.target);
$tgt.parent().css("backgroundColor", "red");
});
on Chrome, event.target is working as expected, on Firefox 3.0.1 it says "event is not defined". How can I work around this?
I've tried the following:
$(this).parent().css()
it says 'object is not a function'
thanks!
Share Improve this question edited Aug 2, 2010 at 1:08 Nick Craver 631k138 gold badges1.3k silver badges1.2k bronze badges asked Feb 11, 2009 at 17:25 balintbalint 3,4318 gold badges43 silver badges51 bronze badges3 Answers
Reset to default 8The event
object gets passed into your click handler by jQuery, so you need to specify it as a parameter.
You also may be able to add the click handler to the parent object and use "$(this)" instead of event.target:
$(...select the parent...).click(function() {
$(this).css("backgroundColor", "red");
});
If the user clicks the parent, or any of its children, it will call your click handler. If you need to only change the background if they actually click on one of the children (not between them, for example) this won't work.
If you want to do anything with events, make sure you pass the event as an argument to the handling function:
$('#foo').click(function(event) {
// ...
});
But ditto what the earlier poster said, you should be able to just use $(this) to get what you want.
$(...selectquery...). .click(function(event) { var $tgt = $(event.target);
$(this).parent().css("...") should work as well
本文标签: javascriptJQuery eventtarget errorStack Overflow
版权声明:本文标题:javascript - JQuery event.target error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743799043a2540967.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论