【问题标题】:Is there a way to set an html5 output to a default value?有没有办法将 html5 输出设置为默认值?
【发布时间】:2012-12-27 07:10:03
【问题描述】:

此代码似乎不起作用:

 <form onload= "x.value=parseInt(a.value)" oninput= "x.value = parseInt(a.value)" id="theinterval" >
    <input class='slidor' type="range" name="a" value="25">
    <h2><output name="x" for="a" ></output></h2>
    </form>

我插入的附加属性是:onload="x.value=parseInt(a.value)" 但它根本没有帮助。

提前致谢

【问题讨论】:

  • 附带说明:oninput 支持不完整 (IE9) 或不支持 (

标签: html properties range onload output


【解决方案1】:

非常简单:

<output name="x" for="a">25</output>

当然 IE 不支持它,所以你又回到了原点。适用于当前适用于 Windows 的 Chrome、Opera、Firefox 和 Safari。我想它也可以在 Mac 版 Safari 上运行,反正比 windows 版本要早几步,但我现在不打算测试它。

不适用于 IE 8,“耶!”。

【讨论】:

    【解决方案2】:

    Form 标签没有任何 onload 属性。所以你需要使用它但不同的eay。您可以借助其他支持 onload 属性的 html 标签(例如:body、div 等)。

    可能的解决方案之一是使用 body 标签的“onload”属性。 它看起来像这样:

    <body onload="o.value = parseInt(a.value)">
    
    <form onsubmit="return true" oninput="o.value = parseInt(a.value)">
      <input id="a" type="range" step="any" value="25">
    
      <output id="o" name="o"></output>
    </form>
    
    </body>
    

    【讨论】:

    • 这个不行,我尝试了div、span、b等的变体
    • 你想让这个“25”从一开始就出现在屏幕上吗?这不是你想要的吗?
    • 是的!我已经看到了!但在我的浏览器上,我的解决方案有效! :S Firefox 不支持 &lt;input type='range'&gt; 根据这个错误 report 但它应该可以在 chrome 上正常工作!能否请您到此jsfiddle repo 重新检查一下,或者您使用的是哪个版本和浏览器?
    【解决方案3】:

    工作代码:

    <form oninput= "x.value = a.valueAsNumber" id="theinterval" >
    <input class='slidor' type="range" name="a" value="25">
    <h2>
    <output Class='outputo' name="x" for="a"></output>
    <p> mins </p>
    </h2>
    </form>
    
    
    <script>
    ;(function() {
    $(".outputo").attr('value', $(".slidor").attr('value') )
    })()
    </script>
    

    【讨论】:

      【解决方案4】:
      <!DOCTYPE html>
      <html lang="pt-br"> 
      <head> 
        <meta charset="utf-8"> 
        <title>output</title>
        <script>
          window.onload = function() {
            var items = document.querySelectorAll("OUTPUT");
            for(var i = 0; i < items.length; i++) {
              items[i].defaultValue = 0;
            }
          }
        </script>
      </head>
      <body>
        <form oninput="x.value=parseInt(a.value)+parseInt(b.value)">
          <input type="number" id="a" value="0">+
          <input type="number" id="b" value="0">=
          <output id="out" name="x" for="a b"></output>
        </form>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-26
        • 2012-08-07
        • 1970-01-01
        • 2010-12-20
        • 1970-01-01
        • 1970-01-01
        • 2016-07-16
        • 1970-01-01
        相关资源
        最近更新 更多