简体   繁体   English

Javascript“this”参考onclick事件无法正常工作

[英]Javascript “this” reference for onclick event not working

I'm trying to call a function with the "onclick" event as so: 我正试图用“onclick”事件调用一个函数,如下所示:

<td id="A1" onclick="move()" class="white"></td>
<td id="A2" onclick="move()" class="painted bp"></td>
<td id="A3" onclick="move()" class="white"></td>

In the function itself, i refer to "this": 在函数本身,我指的是“这个”:

function move(e){
    var myId = this.id;
    alert("myId");
}

When I run the whole thing, the alert says 'undefined'. 当我运行整个事情时,警报说“未定义”。 When I try alert(this) I get [object window]. 当我尝试alert(this)我得到[对象窗口]。 I'm working with IE9, btw. 我正在使用IE9,顺便说一句。 Thanks 谢谢

this is the window object in your code. this是代码中的window对象。

You could pass this as the parameter. 您可以this作为参数传递。

<td id="A1" onclick="move(this)" class="white"></td>

then: 然后:

function move(ele){
    var myId = ele.id;
    alert("myId");
}

When calling a function from an event handler, its this isn't set by the handler (though you can pass this from the handler per Xdazz's answer, or set this per Kyle's answer). 当调用从一个事件处理函数,它的this不被处理程序中设置(尽管你可以通过this每Xdazz的回答处理程序,或设置this每凯尔的答案)。 Another approach is to extract the sender element from the event object associated with the event: 另一种方法是从与事件关联的事件对象中提取sender元素:

function move(e) {
    if (!e)
        e = window.event;
    var sender = e.srcElement || e.target;

    //maybe some nested element.. find the actual table cell parent.
    while (sender && sender.nodeName.toLowerCase() != "td")
        sender = sender.parentNode;

    var myId = sender.id;
    alert(myId);
}
​

You also must pass the event explicitly: 您还必须明确传递事件:

onclick="move(event)"

Note that when the table cell has nested elements they will be the "sender" thus to grab the desired element (which is the table cell) you have to traverse upwards. 请注意,当表格单元格具有嵌套元素时,它们将成为“发送者”,从而获取您必须向上遍历的所需元素(即表格单元格)。 To avoid all this headache see below how to attach the handlers through code . 为了避免所有这些头痛,请参阅下面如何通过代码附加处理程序

Live test case . 现场测试案例

That said, better practice would be to bind the click event through code instead of inline - don't mix HTML with JavaScript. 也就是说,更好的做法是通过代码而不是内联来绑定click事件 - 不要将HTML与JavaScript混合。 To achieve this, have such code: 要实现这一点,请使用以下代码:

window.onload = function() {
    var arrCells = document.getElementsByTagName("td");
    for (var i = 0; i < arrCells.length; i++) {
        var oCell = arrCells[i];
        if (oCell.id && oCell.id.substr(0, 1) == "A") {
            oCell.onclick = move;
        }
    }
}

With the above code in place, you can remove the inline onclick= calls from the HTML and keep your original function - the this will point to the clicked table cell. 使用上面的代码,您可以从HTML中删除内联onclick=调用并保留原始函数 - this将指向单击的表格单元格。

Updated fiddle . 更新了小提琴

<td id="A1" onclick="move.call(this)" class="white"></td>

现在this将引用move函数中的td元素。

Try using the event target instead to get the dom element you are looking for: 尝试使用事件目标来获取您正在寻找的dom元素:

function move(e) {
  alert(e.target);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM