在Edge和IE中通过JavaScript设置输出元素的值

时间:2016-07-21 16:03:43

标签: javascript html internet-explorer-11 microsoft-edge

我有一个def ask_input(prompt, error): while True: value = input(prompt) try: if int(value) > 1: return value except ValueError: print(error) 标记,其值通过JavaScript动态设置。这在Chrome和Firefox中运行得很好,但Microsoft浏览器(IE和Edge)似乎无法识别它,并且output对于这些标记始终为空。

HTML:



value

    document.getElementById("owned").value = "Test 1";    
    document.getElementById("used").value = "Test 2";




当我在Stack Overflow上运行代码片段时,它甚至可以正常工作。

问题:有没有理由在这些浏览器上设置这些值?是否有一个文件我必须导入我的HTML才能在IE / Edge上运行?

3 个答案:

答案 0 :(得分:2)

Microsoft Edge添加了对<output> in version 14 *。

的支持

目前,您可以测试支持,可选择写入textContent属性:

&#13;
&#13;
var used = document.querySelector("#used");
var owned = document.querySelector("#owned");

// Temporarily fall back to textContent
var property = "value" in used ? "value" : "textContent";
used[ property ] = "Test 1";
owned[ property ] = "Test 2";
&#13;
<output class="output" id="owned"></output>  
<output class="output" id="used"></output>
&#13;
&#13;
&#13;

*在撰写本文时,MS Edge 14仅适用于Insiders。它计划于2016年8月2日发布(Windows 10 Anniversary Update

答案 1 :(得分:1)

使用span.innerHTML<output> HTML标记似乎无法在Edge上使用。

答案 2 :(得分:0)

这是IE11中<output>元素的polyfill:

if (window.HTMLOutputElement === undefined) {
  Object.defineProperty(HTMLUnknownElement.prototype, 'value', {
    get: function () {
      if (this.tagName === 'OUTPUT') {
        return this.textContent;
      }
    },
    set: function (newValue) {
      if (this.tagName === 'OUTPUT') {
        this.textContent = newValue;
      }
    }
  });
}

仅经过测试以获取/设置value属性。