javascript - 使用 YUI 更改 IE 8 中脚本标签的内容

标签 javascript internet-explorer yui

以下脚本在 Firefox 中运行良好,但在 IE 8 中崩溃,并显示错误“未知运行时错误”。

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 中处理表格元素时发现了很多对上述问题的引用,但这些情况下的解决方法似乎不适用于 script 标记。

有什么办法可以在 IE 中实现上述功能吗? (我只在 IE 8 中测试过这一点,但我认为问题在 6,7 中是相同的)。

谢谢!!

最佳答案

这里有一个解决方法,可以解决该问题并在 IE 8 中工作(尚未检查 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') );
});

但是,如果有人可以解释为什么设置文本不起作用,如问题中所述,或者您有其他方法可以在不删除/重新添加的情况下完成此操作(这对性能有害吗?),请添加您的答案我会投票!!解决办法使这个问题不再那么紧迫,但我仍然很好奇!!

关于javascript - 使用 YUI 更改 IE 8 中脚本标签的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246865/

相关文章:

css - YUI-3 网格 && 谷歌浏览器 : Overlapping text

asp.net - 如何覆盖外部 css?

javascript - 3层嵌入引号,JS函数不注册变量

html - IE6 溢出问题

javascript - 闪存 fscommands 和 javascript

html - 为什么 DIV 标签在 IE8 和 Firefox 中呈现不同?

javascript - 通过 JQuery 下载 .EXE 文件

javascript - 在 ReactJs 中获取未定义的值

javascript - 如何使用 jqueryui 创建自定义工具提示动画?

javascript - jQuery、Prototype、Extjs、mootools、Scriptaculous、Spry、YUI、DOJO 框架之间有什么区别?