am extending Element
s 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 Element
s, 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
精彩评论