jQuery问题从脚本标签获取文本? [英] Jquery problem getting text from a script tag?

查看:71
本文介绍了jQuery问题从脚本标签获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小的HTML文档:

I have this small HTML document:

<!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>

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

When run using Firefox the alert displays the text contents of the <script type="code"> tag. When run in IE8 it displays nothing.

你知道为什么吗?我很困惑.

Do you know why? I'm stumped.

推荐答案

.html()可能会给您带来更多的运气,如果这样不起作用,请尝试this.innerHtml.但是,我没有对此进行测试.

You might have more luck with .html() and if that doesn't work try this.innerHtml. I did not test this however.

不过,我确实为您的代码提供了另一条提示.如果只需要类型代码的脚本,则可以有一个选择器,而不必在循环中检查属性:

I do have another tip for your code however. If you want only scripts of type code you can have a single selector instead of having to check the attribute in the loop:

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

这篇关于jQuery问题从脚本标签获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆