首页 > javascript > element.removeAttribute的浏览器支持是什么?

element.removeAttribute的浏览器支持是什么? (What is the browser support for element.removeAttribute?)

问题

摘要/详细信息HTML5元素具有糟糕的浏览器支持。因此,我构建了一个非jQuery回退,使其可以在不受支持的浏览器(IE和Edge)中运行。此回退使用element.removeAttribute,但我对此命令的浏览器支持有疑问。我无法在网上找到明确的答案。我尝试过caniuse.comMDN网络文档,但他们没有明确的答案。

我知道它适用于我的(更新的)Firefox版本。任何人有更多的信息?

解决方法

此方法在浏览器中不一致。它至少在MS Edge上是BROKEN,在编写本文时,MDN,W3schools或caniuse都没有提到它的破碎。

基本上,删除布尔属性(如selected或)时,该方法将失败hidden。以下将在Edge上失败:

someDiv.removeAttribute("hidden");

解决方法是在删除属性之前立即将该属性设置为“false”。

someDiv.setAttribute("hidden", "false"); // "thanks" for the nonsense, MS
someDiv.removeAttribute("hidden");

这不是布尔属性应该如何工作,但这就是Edge需要它的方式。鉴于微软即将放弃Edge以支持基于Chromium的替代方案,我们可以预期这个错误将保持不变,以及多年来使我们的代码混乱的解决方法。

问题

The summary/details HTML5 element has terrible browser support. Therefore I built a non-jQuery fallback to make it work in non-supported browsers (IE and Edge). This fallback uses element.removeAttribute, but I am in doubt about the browser support of this command. I cannot find a definitive answer online. I have tried caniuse.com and MDN web docs, but they have no clear answers.

I know it works in my (updated) version of Firefox. Anyone has more info?

解决方法

This method does not work consistently across browsers. It is BROKEN on MS Edge at least, and its brokenness is not mentioned by MDN, W3schools or caniuse at time of writing.

Basically, the method will fail when removing boolean attributes such as selected or hidden. The following will fail on Edge:

someDiv.removeAttribute("hidden");

Workaround is to set the attribute to "false" immediately before removing it.

someDiv.setAttribute("hidden", "false"); // "thanks" for the nonsense, MS
someDiv.removeAttribute("hidden");

This is not how boolean attributes are supposed to work, but that's how Edge requires it. Given that Edge is about to be dropped by Microsoft in favour of a Chromium-based alternative, we can expect this bug to remain unfixed, and the workaround to clutter our code for years.

相似信息