event.target.id 仅适用于 Chrome
最奇怪的事情。
我有一个函数调用:
var clicked_el = event.target.id;
此函数在 FireFox 中的这一行处中断。它在 ie9 中有效,但在 8 中无效(因为 ie8 使用 srcElement 代替)。每次尝试获取发起事件的元素的 id 都完全失败了。
我认为 jQuery 有一种方法可以消除浏览器与 jQuery.Event 的不一致,但我无法让任何东西发挥作用。
谁能告诉我一个更好的浏览器一致方法来做到这一点?
这是函数,它是我的主干视图的一部分。
open: function () {
iconState = true;
var self = this;
var clicked_el = event.target.id;
$('div.hr, div#footer_icons').animate({
top : '-=300'
}, 500, function () {
if (innerContentIsVisible === false) {
$('#a_close').fadeIn(500);
}
});
$('div#article').animate({
opacity : 0
}, 500,function () {
self.load_content(clicked_el);
});
},
更新
open
函数是从另一个函数调用的,当我调用 open
函数时,我忘记传递事件参数,这就是为什么它返回未定义
。
这是现在传递 event
参数的函数:
click: function(event) {
if (iconState === false) {
this.open(event);
} else {
var self = this;
var clicked_el_icon = event.target.id;
$('div#loaded_content').fadeOut(250, function() {
self.load_content(clicked_el_icon);
});
}
},
open: function (event) {
iconState = true;
alert(event);
var self = this;
var clicked_el = event.target.id;
$('div.hr, div#footer_icons').animate({
top : '-=300'
}, 500, function () {
if (innerContentIsVisible === false) {
$('#a_close').fadeIn(500);
}
});
$('div#article').animate({
opacity : 0
}, 500,function () {
self.load_content(clicked_el);
});
},
谢谢...我不会忘记这个教训。
The wierdest thing.
I have a function which calls:
var clicked_el = event.target.id;
This function breaks at this line in FireFox. It works in ie9, but not in 8 (because ie8 uses srcElement instead). Every attempt to grab the id of the element which initiated the event has been a complete flameout on my end.
I think there is a way that jQuery smoothes out browser incosistencies with jQuery.Event
but I can't get anything working.
Can anyone tell me a better browser consistent method to do this?
here is the function, it's part of my backbone view.
open: function () {
iconState = true;
var self = this;
var clicked_el = event.target.id;
$('div.hr, div#footer_icons').animate({
top : '-=300'
}, 500, function () {
if (innerContentIsVisible === false) {
$('#a_close').fadeIn(500);
}
});
$('div#article').animate({
opacity : 0
}, 500,function () {
self.load_content(clicked_el);
});
},
UPDATE
The open
function was being called from another function, and I forgot to pass the event paramater when I invoked the open
function, which is why it was returning undefined
.
here is the function which now passes the event
param:
click: function(event) {
if (iconState === false) {
this.open(event);
} else {
var self = this;
var clicked_el_icon = event.target.id;
$('div#loaded_content').fadeOut(250, function() {
self.load_content(clicked_el_icon);
});
}
},
open: function (event) {
iconState = true;
alert(event);
var self = this;
var clicked_el = event.target.id;
$('div.hr, div#footer_icons').animate({
top : '-=300'
}, 500, function () {
if (innerContentIsVisible === false) {
$('#a_close').fadeIn(500);
}
});
$('div#article').animate({
opacity : 0
}, 500,function () {
self.load_content(clicked_el);
});
},
Thanks... I will not forget this lesson.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将“event”声明为“open”处理函数的参数。
You have to declare "event" as the parameter for the "open" handler function.