【问题标题】:Problems with option selection in Chrome and OperaChrome 和 Opera 中的选项选择问题
【发布时间】:2013-08-01 13:19:23
【问题描述】:

我的代码有问题,它在 Firefox 和 Internet Explorer 中运行良好,但不适用于 Opera 和 Chrome 浏览器...

<script>
function planetselect()
{
optionen=document.getElementById('pstart').options;
for(i=0;i<optionen.length;i++)
{
if(optionen[i].value==67080)
{
  optionen[i].setAttribute('selected','selected');
  }
}
optionen=document.getElementById('pdest').options;
for(i=0;i<optionen.length;i++)
{
if(optionen[i].value==67080)
{
  optionen[i].setAttribute('selected','selected');
  }
}
}</script>

【问题讨论】:

    标签: javascript html google-chrome firefox dom


    【解决方案1】:

    改变

    optionen[i].setAttribute('selected','selected');
    

    optionen[i].selected = true;
    

    更一般地,避免使用setAttribute 来更改DOM 属性。有时有效,有时无效。

    来自the MDN

    使用 setAttribute() 修改某些属性,尤其是值 在 XUL 中,工作不一致,因为属性指定了默认值 价值。要访问或修改当前值,您应该使用 特性。例如,使用 elt.value 代替 elt.setAttribute('value', val).

    【讨论】:

      【解决方案2】:

      您确定关闭了&lt;script&gt; 标签吗?我真的看不出你发布的代码有问题,所以要么你没有关闭你的标签,要么你的 optionenoptions 变量不存在或无效

      此外,您应该知道 chrome 有一个 javascript 控制台,可以显示您遇到的任何错误。要打开它,它是ctrl-shift-j。这应该对你有很大帮助。

      【讨论】:

        猜你喜欢
        • 2013-11-16
        • 2016-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-26
        相关资源
        最近更新 更多