从脚本标记获取文本的Jquery问题?

我有这个小HTML文档:

   HTML Test   $(document).ready(function() { $("script").each(function() { if($(this).attr("type") == "code") { alert($(this).text()); } }); });     var Text = "Text";    

使用Firefox运行时,警报会显示标记的文本内容。 在IE8中运行时,它什么都不显示。

你知道为什么吗? 我很难过。

你可能会有更多的运气与.html(),如果这不起作用尝试this.innerHtml 。 但是我没有测试过这个。

不过,我的代码还有其他提示。 如果只需要类型代码的脚本,则可以使用单个选择器,而不必检查循环中的属性:

 $("script[type=code]").each(function() { alert($(this).html()); alert(this.innerHtml); }); 

使用.html()代替更好的支持ie。

您还可以查看: http : //docs.jquery.com/Plugins/Metadata ,用于在脚本标记中存储信息。

    HTML Test  $(document).ready(function() { $("script").each(function() { if($(this).attr("type") == "code") { alert($(this).html()); } }); });