[英]Deleting a row from an HTML table in javascript
I have javascript code that manages an HTML table. 我有管理HTML表的JavaScript代码。 The code needs to be able to delete a row from an HTML table.
该代码需要能够从HTML表中删除一行。
I am currently using the following code to delete the row: 我目前正在使用以下代码删除该行:
var rowToDelete = ...;
if (rowToDelete)
rowToDelete.remove();
This works fine in Firefox & Chrome, however, when I load the page in IE 11 & bring up the javascript debugger, it displays 这在Firefox和Chrome中正常运行,但是,当我在IE 11中加载页面并调出JavaScript调试器时,它将显示
Object doesn't support property or method 'remove'
对象不支持属性或方法“删除”
I haven't tried this code in earlier versions of IE yet. 我尚未在IE的早期版本中尝试过此代码。
How can I do this in a cross-browser manner? 我该如何以跨浏览器的方式执行此操作? My page does have jQuery included so I can use a jQuery method.
我的页面上确实包含jQuery,所以我可以使用jQuery方法。
Chrome supports .remove()
on elements. Chrome在元素上支持
.remove()
。
You should do: 你应该做:
rowToDelete.parentNode.removeChild(rowToDelete);
If you want this functionality in IE you can add the function to the HTMLElement prototype. 如果要在IE中使用此功能,可以将该功能添加到HTMLElement原型中。
HTMLElement.prototype.remove = function (){
this.parentNode.removeChild(this);
}
Make sure your rowToDelete is an jQuery Object, like this: 确保rowToDelete是一个jQuery对象,如下所示:
var rowToDelete = $('tr');
rowToDelete.remove();
如果这是dom元素,则可以执行
$(rowToDelete).remove();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.