开发者

how do you extend the stack element in the dom?

开发者 https://www.devze.com 2023-02-26 14:14 出处:网络
am extending Elements successfully, eg, Element.prototype.ancestorByTagName = function( tagName ) { ... }

am extending Elements successfully, eg,

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

but cannot figure out how to extend specifically the Select element

for example, each of the following fails:

Select.prot开发者_JAVA百科otype.appendOption = function( value, label, select_value ) { ... }
Element.Select.prototype.appendOption = function( value, label, select_value ) { ... }

am at a loss.

in this case it makes sense to limit the new method appendOption it to Select versus all Elements, since only Select can have Option children. Select's add method is pretty lame as well.


Not sure where you got Element.Select from but it's not a native object. You want HTMLSelectElement instead (MDC reference).


Note: do read this article, as pointed out in the comments, on why not to extend the DOM.


Here is something that worked in 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>

and gave me this in IE8

Webpage error details

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

精彩评论

暂无评论...
验证码 换一张
取 消