我试图使为IE8开发的简单Java脚本页面与IE9及更高版本兼容。当我尝试使用原型的getValue()方法时,select标记给出“ TypeError:对象不支持属性或方法'getValue'”。该页面在IE 9的所有版本中都能正常工作,但对于不具有ADMIN权限的县县用户而言,在IE9中除外。
有人可以帮我解决这个问题吗?
<!DOCTYPE html>
<html>
<head>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript" SRC="prototype.js"></SCRIPT>
</head>
<body style="width:500px">
<form id="example" action="#" onsubmit="return false">
<div id="container">
<select size="7" style="width:auto;" name="commonRTSelect" id="commonRTSelect" multiple>
<option value="volvo" selected>Volvo</option>
<option value="saab" selected>Saab</option>
<option value="mercedes" selected>Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="text" style="width:auto;" name="fname" id="fname" value="madhu">
<br>
</div>
</form>
<input type="button" value="Toggle" onclick="showResult();"/>
<script>
function showResult() {
var elems = $('container').select('input','select','textarea');
elems.each( function(e){
if(e instanceof HTMLElement) {
alert("Yes.."+e.name+" is an instance of HTMLElement");
}
else {
alert("No "+e.name+" is not an instance of HTMLElement. it is " + e);
}
});
try{
alert("Dropdown Value>> " + $('commonRTSelect').getValue());
}
catch(e) {
alert(e);
}
}
</script>
</body>
</html>
当我调试此问题时,我发现select标签的类型为DispHTMLWndSelectElement。无法在此处附加屏幕截图。
最佳答案
代替,
$('commonRTSelect').getValue()
...请尝试,
$F('commonRTSelect')
在内部,他们应该调用相同的函数,实际上是checking the element's tag name。这里的问题是
DispHTMLWndSelectElement
尚未从getValue()
继承HTMLSelectElement
函数,因此解决方案是不调用实例方法。至于为什么IE决定破坏兼容性,还是有人猜测。也许它检测到不在文档字符集中的字符,然后退回到另一种渲染模式。如果不比较页面资源和ouigii板,将很难分辨。
关于javascript - 为什么IE9中的Select Element类型为DispHTMLWndSelectElement但不是HTMLSelectElement?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25269557/