JavaScript:getElementById在IE8中不起作用 [英] JavaScript: getElementById not working in IE8
问题描述
我有这个非常简单的JavaScript来显示跨度中的选定选项。它在除IE8以外的所有浏览器都能正常工作。
代码 -
< select onchange =searchdisplay(this.value)>
< option> kill< / option>
< option> bill< / option>
< option>按< / option>
< option> torentino< / option>
< option>是< / option>
< option> not< / option>
< option>良好< / option>
<选择>
< span id =container>
< span>
脚本 -
<脚本>
函数searchdisplay(val)
{
var div = document.getElementById('container');
div.innerHTML = val;
}
< / script>
对此有何解决方案?感谢。
问题在于IE8中的 this.value
至: -
< select onchange =searchdisplay(this.options [this.selectedIndex] .value)>
,它应该可以正常工作。
在你当前的代码中
修正你的标记,就像别人在评论中说的一样。
< select onchange =searchdisplay(this.options [this.selectedIndex] .value)>
< option> kill< / option>
< option> bill< / option>
< option>按< / option>
< option> torentino< / option>
< option>是< / option>
< option> not< / option>
< option>良好< / option>
< / select>
< span id =container>< / span>
理想的方法是为值添加
属性选择选项。并做 this.value
这可能会保证工作。
I've this very simple JavaScript to display the selected option in the span. It works fine in all the browsers except IE8.
Code -
<select onchange="searchdisplay(this.value)">
<option>kill</option>
<option>bill</option>
<option>by</option>
<option>torentino</option>
<option>is </option>
<option>not </option>
<option>good</option>
<Select>
<span id="container">
<span>
Script -
<script>
function searchdisplay(val)
{
var div = document.getElementById('container');
div.innerHTML = val;
}
</script>
Any solution for this? Thanks.
Issue is with this.value
in IE8 change it to :-
<select onchange="searchdisplay(this.options[this.selectedIndex].value)">
and it shall work fine.
In your current code
Fix your markup too as others said in the comments.
<select onchange="searchdisplay(this.options[this.selectedIndex].value)">
<option>kill</option>
<option>bill</option>
<option>by</option>
<option>torentino</option>
<option>is</option>
<option>not</option>
<option>good</option>
</select>
<span id="container"></span>
Ideal way is to add value
attribute for the select options. and do this.value
which probably is guaranteed to work.
这篇关于JavaScript:getElementById在IE8中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!