There is no innertext in Firefox, so it is difficult to obtain the option text (not the value) in Firefox. I will summarize the solutions and code in my project. Please advise.
Knowledge point:
0. Why innertext? Security problems
1. Extend attributes for the Firefox Dom Model
2. The currentstyle attribute can get the actual style status.
3. The display mode is taken into account when ie implements innertext. If it is block, a line break is added.
4. Why not use textcontent? Because textcontent does not consider the display method of elements, it is not fully compatible with IE.
Code:All tests passed in IE6, 7,8, and Firefox 2 and 3.
<HTML>
<Head>
<Script language = "JavaScript">
// If your browser is IE, return true. If is others, like Firefox, return false.
Function isie () {// ie?
If (window. Navigator. useragent. tolowercase (). indexof ("MSIE")> = 1)
Return true;
Else
Return false;
}
// If is Firefox, We need to rewrite its innertext attribute.
If (! Isie () {// Firefox innertext define
Htmlelement. Prototype. _ definegetter _ ("innertext ",
Function (){
VaR anystring = "";
VaR Childs = This. childnodes;
For (VAR I = 0; I <Childs. length; I ++ ){
If (Childs [I]. nodetype = 1)
Anystring + = Childs [I]. tagname = "Br "? '\ N': Childs [I]. textcontent;
Else if (Childs [I]. nodetype = 3)
Anystring + = Childs [I]. nodevalue;
}
Return anystring;
}
);
Htmlelement. Prototype. _ definesetter _ ("innertext ",
Function (stext ){
This. textcontent = stext;
}
);
}
Function getselectedtext (name ){
VaR OBJ = Document. getelementbyid (name );
For (I = 0; I <obj. length; I ++ ){
If (OBJ [I]. Selected = true ){
Return OBJ [I]. innertext;
}
}
}
Function chk (){
VaR objtext = getselectedtext ("myselect ");
Alert ("seleted option's text is:" + objtext );
VaR objvalue = Document. getelementbyid ("myselect"). value;
Alert ("seleted option's value is:" + objvalue );
}
</SCRIPT>
</Head>
<Body>
<Select id = "myselect">
<Option value = "1111"> my 1111 hahaha </option>
<Option value = "2222"> my 2222 </option>
<Option value = "3333"> my 3333 </option>
<Option value = "4444"> my 4444 </option>
</SELECT>
<Input type = "button" name = "button" value = "see result" onclick = "javascript: chk ();">
</Body>
</Html>
Of course, if you are solely targeting the drop-down box, you do not need to rewrite innertext. The following code can also be used. Innertext is rewritten to be compatible with HTML elements other than the drop-down box.
<HTML>
<Head>
<Script language = "JavaScript">
Function chk (){
// Var objtext = getselectedtext ("myselect ");
VaR OBJ = Document. getelementbyid ("myselect ");
VaR objtext = obj. Options [obj. selectedindex]. Text
Alert ("seleted option's text is:" + objtext );
VaR objvalue = Document. getelementbyid ("myselect"). value;
Alert ("seleted option's value is:" + objvalue );
}
</SCRIPT>
</Head>
<Body>
<Select id = "myselect">
<Option value = "1111"> my 1111 hahaha </option>
<Option value = "2222"> my 2222 </option>
<Option value = "3333"> my 3333 </option>
<Option value = "4444"> my 4444 </option>
</SELECT>
<Input type = "button" name = "button" value = "see result" onclick = "javascript: chk ();">
</Body>
</Html>