【问题标题】:.innerHTML not working in IE.innerHTML 在 IE 中不起作用
【发布时间】:2013-08-26 08:54:47
【问题描述】:

我正在处理基于 onChange 事件中的另一个选择框填充的选择框。它适用于除 IE 以外的所有浏览器。

function getVersion(str){
if (str==""){
    document.getElementById("vSelect").innerHTML="<option value=''>Please Select a product</option>";
return;
}

if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    document.getElementById("vSelect").innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("GET","includes/version.php?product="+str,true);
xmlhttp.send();
}

它改变了这一点

<label>Version: </label>
<select id='vSelect'>
    <option value=''>Please Select a product</option>
</select>

在 IE 中,当 onchange 触发时,它只会清除选择的文本。有任何想法吗?我正在尝试在不加载库的情况下执行此操作。

【问题讨论】:

  • 尽量不要使用innerHTML,因为innerHTML 太可怕了。相反,创建一个&lt;option&gt; 元素并添加它:var newOption = document.createOption('option'); newOption.appendChild(document.createTextNode('Please select a product')); document.getElementById('vSelect').appendChild(newOption); 它可能不起作用(我无法测试它),但它不会更好地工作。
  • @minitech 看看那个,谢谢 :-)

标签: javascript html internet-explorer


【解决方案1】:

在 IE 中存在一个已知错误,Microsoft 确认“innerHTML”无法处理 IE 中的选择对象,并在此 page 中声明。他们给出了解决方法,建议在 select 标签周围包裹一个 div 并使用 innerHTML。有关解决方法,请参见同一页面。希望这会有所帮助。

【讨论】:

  • 感谢您的建议,我试着研究一下它的实现。我不知道这家伙对每个人都投了反对票,但这对我来说是很好的信息。
【解决方案2】:

您不能在 IE 中更改 &lt;select&gt; 的内容。您必须替换整个 &lt;select&gt; 元素

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多