gpt4 book ai didi

javascript - IEs对 'this'的理解

转载 作者:行者123 更新时间:2023-12-02 05:55:35 27 4
gpt4 key购买 nike

在此代码中,Firefox 将“this”视为被单击的元素,并通过正确的方式传递 href 属性。

IE 似乎认为“this”是 [object window]。我怎样才能让它在两种浏览器中以相同的方式工作?

注意:jQuery 会很可爱,但不是这个项目的选项

var printElem = getElementsByClassName('print', 'a');
for(i in printElem){
ObserveEvent(printElem[i], 'click', function(e){
window.open(this.href, '', 'location=0,menubar=1,resizable=1,scrollbars=1,width='+810+',height='+700);
cancelEvent(e);
});
}

最佳答案

您可以使用 event target ,像这样:

    ObserveEvent(printElem[i], 'click', function(e){
var target = e.target || e.srcElement;
window.open(target.href, '', 'location=0,menubar=1,resizable=1,scrollbars=1,width='+810+',height='+700);
cancelEvent(e);
});

关于javascript - IEs对 'this'的理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1466875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com