簡體   English   中英

如何擴展dom中的stack元素?

[英]how do you extend the stack element in the dom?

正在成功擴展Element ,例如,

Element.prototype.ancestorByTagName = function( tagName ) { ... }

但無法弄清楚如何專門擴展Select元素

例如,以下每個失敗:

Select.prototype.appendOption = function( value, label, select_value ) { ... }
Element.Select.prototype.appendOption = function( value, label, select_value ) { ... }

茫然。

在這種情況下,將新方法appendOption限制為Select而不是All ElementappendOption的,因為只有Select可以具有Option子項。 Selectadd方法也很la腳。

不確定從何處獲得Element.Select ,但這不是本機對象。 您需要HTMLSelectElementMDC參考 )。


注意 :請務必閱讀本文 ,如注釋中所指出的,有關為什么擴展DOM的內容。

這是在Firefox 4中有效的功能

<script>
if (HTMLSelectElement)  HTMLSelectElement.prototype.addOption = function(text,value) {
  this.options[this.options.length]= new Option(value,text)
}
else alert("It's not called HTMLSelectElement")
window.onload=function() {
  document.getElementById('sel1').addOption("Hi","There")
}
</script>
<select id="sel1"></select>

並在IE8中給了我

網頁錯誤詳細信息

Message: 'HTMLSelectElement' is undefined
Line: 2
Char: 1
Code: 0
URI: file:///C:/TEMP/addoption.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM