在Internet Explorer中使用jQuery this.remove()-vs.- $('#id').remove()(IE 9+)

pem*_*oke 7 javascript jquery internet-explorer

为什么它this.remove()在IE9 +中不起作用?

<input type="button" value="Next1" id="nextButton1">
<br>
<input type="button" value="Next2" id="nextButton2">

$('#nextButton1').on('click', function() {
   this.remove(); // works in all browsers but IE9+
});

$('#nextButton2').on('click', function() {
   $('#nextButton2').remove(); //works in all browsers
});
Run Code Online (Sandbox Code Playgroud)

JSFiddle现场版

Pet*_*rKA 6

那是因为您使用的ChildNode.remove()是所有浏览器都不支持的方法.

this ---> refers to a node. //WARNING: This is an experimental technology
Run Code Online (Sandbox Code Playgroud)

jQuery的.remove()方法,不过是跨浏览器的,因此使用它,你必须包装this$(...)这样的:

$(this).remove();
Run Code Online (Sandbox Code Playgroud)

ChildNode.remove()浏览器兼容性

this.remove() 以下桌面浏览器支持:

- Chrome 23+
- Firefox 23+
- Opera 10+
- Safari 7+
Run Code Online (Sandbox Code Playgroud)