有一个动态创建的<style>
标签,我正在将一个额外的 css 连接到它。这是用于动态加载模块的更大框架的一部分。
最初它创建了新的样式标签,但由于著名的“31 样式表限制”,这不再是一个选项,所以我决定回收现有的样式标签(除非它们包含太多规则或者如果新字符串包含@import,在这种情况下,无论如何都必须创建一个新的)。
无论如何,我在所有主流浏览器(FF2+、Chrome14+、Opera10+、IE7+、Safari 4.0+)中都能正常工作,现在 IE6 让我头疼。
我在其他情况下熟悉此错误,但我从未在这种情况下见过。另外,不要打扰任何 IE6-timeywimey,我很清楚这一点。但不幸的是,这是一个异常(exception)情况,仍然需要 IE6 支持。
请注意,括号计数仅用于估算和调试目的。 try/catch 不在 cssText +=
附近最初,我添加它是因为我发现它在 IE6 中抛出异常。
最佳答案
我找到了我自己问题的答案。事实证明,IE6 有一个模糊的安全措施,它不允许操纵由其他脚本间接创建的元素(即使它们来自完全相同的来源/域)。
在本例中为 simplified method工作正常,但是 actual code失败了。唯一的区别是实际代码使用 jQuery 来存储数据、创建元素和插入元素。最后一个操作(插入元素)是问题所在。
我变了
/**
* @param {jQuery|HTMLElement} $nextnode
*/
function addStyleTag( text, $nextnode ) {
var s = document.createElement( 'style' );
// IE: Insert into document before setting cssText
if ( $nextnode ) {
// If a raw element, create a jQuery object, otherwise use directly
if ( $nextnode.nodeType ) {
$nextnode = $( $nextnode );
}
$nextnode.before( s );
/* ... */
到:
/**
* @param {jQuery|HTMLElement} nextnode
*/
function addStyleTag( text, nextnode ) {
var s = document.createElement( 'style' );
// IE: Insert into document before setting cssText
if ( nextnode ) {
// If a jQuery object, get raw element, otherwise use directly
if ( nextnode.jquery ) {
nextnode = nextnode.get( 0 );
}
nextnode.parentNode.insertBefore( s, nextnode );
/* ... */
这就解决了问题。
关于javascript - IE6 在连接到 styleSheet.cssText 时抛出 "Access is denied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12586482/