javascript a.nodeName 是未定义的 Jquery 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8817148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:40:18  来源:igfitidea点击:

a.nodeName is undefined Jquery error

javascriptjquerynodename

提问by Barney

a.nodeName is undefined

a.nodeName 未定义

I've looked this up but the explanations didn't seem at all clear to me.

我查了一下这个,但解释对我来说似乎一点也不清楚。

function deleteThisRow() {
    $(this).closest('tr').fadeOut(400, function(){
        $(this).remove();
    });
}
<tr>
    <td>blah blah blah</td>
    <td>
        <img src="/whatever" onClick="deleteThisRow()">
    </td>
</tr>

回答by Rory McCrossan

The thiskeyword in your function does not refer to the element which was clicked on. By default it would refer to the highest element in the DOM, which would be the window.

this您的函数中的关键字不是指被点击的元素。默认情况下,它会引用 DOM 中最高的元素,即window.

To fix this you can use an unobtrusive event handler, instead of an outdated on*event attribute, as they run under the scope of the element which raised the event. Try this:

要解决此问题,您可以使用不显眼的事件处理程序,而不是过时的on*事件属性,因为它们在引发事件的元素范围内运行。试试这个:

$("tr td img").click(deleteThisRow);

function deleteThisRow() {
  $(this).closest('tr').fadeOut(400, function() {
    $(this).remove();
  });
}
img {
  width: 20px;
  height: 20px;
  border: 1px solid #C00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>blah blah blah 1</td>
    <td><img src="/whatever"></td>
  </tr>
  <tr>
    <td>blah blah blah 2</td>
    <td><img src="/whatever"></td>
  </tr>
  <tr>
    <td>blah blah blah 3</td>
    <td><img src="/whatever"></td>
  </tr>
</table>

回答by Sudhir Bastakoti

Try:

尝试:

$(document).ready(function() {
    $("img").click(function() {
        $(this).closest('tr').fadeOut(400, function(){
            $(this).remove();
        });
    });
});

回答by Raymond Wachaga

I encountered a similar issue and the solution was to switch from an arrow function to a traditional named function. Sometimes old is gold but I'm sure there is a root cause somewhere.

我遇到了类似的问题,解决方案是从箭头函数切换到传统的命名函数。有时旧是金,但我确信某处有根本原因。

This didn't work:

这不起作用:

$(document).ready(() => {
  $('#client_id').change(() => {
    const clientId = $(this).val();
    console.log(clientId);
  });
});

The console print out was the error:

控制台打印出来的是错误:

TypeError: i.nodeName is undefined

类型错误:i.nodeName 未定义

On further investigation it turned out '$(this)' was calling the window object rather than the select element. (As pointed out by Rory above: https://stackoverflow.com/a/8817193/5925104)

进一步调查发现'$(this)'正在调用window对象而不是select元素。(正如 Rory 上面指出的:https: //stackoverflow.com/a/8817193/5925104

This worked. A value was printed out to the console.

这奏效了。一个值被打印到控制台。

$(document).ready(() => {
  $('#client_id').change(function changeClient() {
    const clientId = $(this).val();
    console.log(clientId);
  });
});