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

Gar*_*hby 4 jquery

我有这个小HTML文档:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>HTML Test</title>

        <script type="text/javascript" src="jquery-1.3.2.min.js"></script>

        <script type="text/javascript">
            $(document).ready(function()
            {
                $("script").each(function()
                {
                    if($(this).attr("type") == "code")
                    {
                        alert($(this).text());
                    }

                });
            });
        </script>

    </head>

    <body>

<script type="code">
var Text = "Text";
</script>

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

使用Firefox运行时,警报会显示<script type="code">标记的文本内容.在IE8中运行时,它什么都不显示.

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

Dar*_*o Z 8

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

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

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