javascript – a.nodeName未定义Jquery错误

前端之家收集整理的这篇文章主要介绍了javascript – a.nodeName未定义Jquery错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

a.nodeName is undefined

我看了这个,但解释似乎对我来说都不清楚.

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

解决方法

函数中的this关键字不引用被单击的元素.默认情况下,它将引用DOM中的最高元素,即窗口.

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

@H_301_8@$("tr td img").click(deleteThisRow); function deleteThisRow() { $(this).closest('tr').fadeOut(400,function() { $(this).remove(); }); } @H_301_8@img { width: 20px; height: 20px; border: 1px solid #C00; } @H_301_8@<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>
原文链接:https://www.f2er.com/jquery/156398.html

猜你在找的jQuery相关文章