在ie8中使用YUI更改脚本标签的内容

Changing the contents of a script tag in IE 8 using YUI

本文关键字:标签 脚本 ie8 YUI      更新时间:2023-09-26

下面的脚本在Firefox中运行正常,但在IE 8中崩溃,错误为"Unknown runtime error"

YUI().use('node', function (Y) {
    if (Y.one('#testel') == null) {
        Y.one('head').append(Y.Node.create('<script id="testel" />'));
    }
    Y.one('#testel').set('text', 'Wish this would work in IE!');
    console.log( Y.one('#testel').get('text') );
});

我在IE中尝试过这样的事情:

Y.one('#testel').set('innerHtml', 'Wish this would work in IE!');
Y.one('#testel').set('html', 'Wish this would work in IE!');
Y.one('#testel').setContents('Wish this would work in IE!');

唯一的区别是有时它给出了更有用的错误"意外调用方法或属性访问",这让我认为脚本标签的文本不能在IE中更改?

当我在IE中处理表元素时,我发现了很多关于上述问题的参考,但在这些情况下的工作似乎并不适用于脚本标签。

有没有办法在IE中实现以上?(我只在ie8中测试过,但我认为问题在6,7中是相同的)。

谢谢! !

这是一个解决问题的工作,并在ie8中工作(还没有检查6,7)。

似乎你只能设置文本,当你创建一个脚本标签,所以,而不是试图像问题中的代码更新文本,我只是删除并重新创建脚本标签,任何时候我想改变文本(这也适用于FF)。

例如:

YUI().use('node', function (Y) {
    var writeToScript = function(text) {
        if (Y.one('#asdf') != null)
            Y.one('#asdf').remove();
        Y.one('head').append(Y.Node.create("<script id='asdf'>"+ text + " </script>"));
    }
    writeToScript("Hello there!");
    console.log( Y.one('#asdf').get('innerHTML') );
    writeToScript("How are you?");
    console.log( Y.one('#asdf').get('innerHTML') );
    writeToScript("Goodbye");
    console.log( Y.one('#asdf').get('innerHTML') );
});

然而,如果有人能解释为什么设置文本不像问题中所述的那样工作,或者如果你有另一种方法来实现这一点而不删除/重新添加(这对性能不利吗?),请添加你的答案,我会投票!!周围的工作使这个不那么紧迫,但我仍然很好奇!!

相关文章: