2017-02-03 26 views
1

我有这行代码,但似乎Internet Explorer不能识别“删除”功能。删除在Internet Explorer中不工作的选项索引

this.options [this.selectedIndex] .remove();

错误说'对象不支持删除功能',任何想法如何在IE中做到这一点?

注:此=选择元素,它无论是在Firefox和Chrome

+0

看到这个:http://stackoverflow.com/questions/20428877/javascript-remove-doesnt-work-in-ie –

回答

1

IE工程不支持.remove(),你需要使用element.parentNode.removeChild(element)或像下方的填充工具。请参阅:

https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove

// from:https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md 
    (function (arr) { 
     arr.forEach(function (item) { 
      item.remove = item.remove || function() { 
       this.parentNode.removeChild(this); 
      }; 
     }); 
    })([Element.prototype, CharacterData.prototype, DocumentType.prototype]); 
0

IE不支持删除()在Javascript。只在jQuery中。

如果你想使用remove()方法在Javascript中把你的代码上面这个下面的代码调用删除方法:

// Create remove function if not exist 
    if (!('remove' in Element.prototype)) { 
    Element.prototype.remove = function() { 
    if (this.parentNode) { 
     this.parentNode.removeChild(this); 
    } 
}; 
} 
// Call remove() according to your need 
myVar.remove(); 

更多信息上removeChild之():https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove

编码愉快! :)

相关问题