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

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

我有一个 output 标记,其值是通过 JavaScript 动态设置的。这在 Chrome 和 Firefox 中工作得很好,但 Microsoft 浏览器(IE 和 Edge)似乎无法识别它,并且这些标签的 value 始终为空。

HTML:

    document.getElementById("owned").value = "Test 1";    
    document.getElementById("used").value = "Test 2";
    <output class="output" id="owned"></output>
    <output class="output" id="used"></output>

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

问题:是否有任何原因导致这些浏览器上未设置这些值?是否有一个文件必须导入到 HTML 中才能在 IE/Edge 上运行?

最佳答案

Microsoft Edge 添加了对 <output> 的支持in version 14 *.

现在,您可以测试支持,可以选择写入 textContent属性代替:

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";
<output class="output" id="owned"></output>  
<output class="output" id="used"></output>

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

关于javascript - 在 Edge 和 IE 中通过 JavaScript 设置输出元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38509088/

相关文章:

css - IE11 边框样式问题,当双方是不同的颜色

javascript - 是否可以修改 Location 对象的现有属性,例如 "location.href"?

javascript - 如何使用javascript获取表单名称和输入?

javascript - d3.js 通过 dbclick 事件打开新选项卡

html - 如何使用 Excel VBA 查找/提取属性大小为 "font"的 HTML ="+1"元素

javascript - 我如何将 mustache 中的变量大写

javascript - 3d 鱼眼 - 如何在鼠标悬停时使条形变宽?

javascript - 使用 Angular 进行动态路由 - 如何使用此 JSON?

javascript - jQuery 窗口滚动无法在没有警报的情况下工作

html - flex 属性在 IE 中不起作用