JavaScript:getElementById在IE8中不起作用 [英] JavaScript: getElementById not working in IE8

查看:106
本文介绍了JavaScript:getElementById在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.

Demo

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆