Chrome错误与NO_MODIFICATION_ALLOWED_ERR DOM异常7 [英] Chrome error with NO_MODIFICATION_ALLOWED_ERR DOM Exception 7
问题描述
我有以下代码插入一个样式到DOM(有一个用例注入风格到DOM所以请不要问为什么或说要加载css在.css文件)。
I have below code to insert a style into DOM (there is a use case for injecting style into DOM so please don't ask why or say to load the css in .css file).
<script type="text/javascript">
window.onload = function()
{
var bmstyle = document.createElement('style');
bmstyle.setAttribute('type', 'text/css');
var styleStr = "#test-div {background:#FFF;border:2px solid #315300;";
bmstyle.innerHTML = styleStr;
document.body.appendChild(bmstyle);
}
</script>
如果我在Firefox中运行,它可以正常工作。但我在Google Chrome中遇到此错误:
If I run in Firefox, it works fine. But I got this error in Google Chrome:
Line bmstyle.innerHTML = styleStr;
Uncaught Error: NO_MODIFICATION_ALLOWED_ERR: DOM Exception 7
有人有修复吗?感谢
推荐答案
我认为这是因为你在其他地方使用 innerHTML
您正在使用XML语法。尝试:
I think it's because you are using innerHTML
when everywhere else you are using XML syntax. Try:
bmstyle.nodeValue = styleStr;
建议2:
因为你正在尝试设置一个元素的innerHTML尚未在HTML DOM中。如果是这样,我的第一个建议仍然应该支持,或者你可以去:
It might also be because you are trying to set the innerHTML of a an element not yet in the HTML DOM. If that's the case, my first suggestion should still hold up, or you can go with:
document.body.appendChild(bmstyle);
bmstyle.innerHTML = styleStr;
我不知道你是否需要一条线来回收元素,如果bmstyle仍然指向它。
I'm not sure if you'd need a line inbetween to reclaim the element or if the bmstyle would still point to it.
这篇关于Chrome错误与NO_MODIFICATION_ALLOWED_ERR DOM异常7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!