将类放在脚本标记上有什么好处? [英] Is there a benefit to putting a class on a script tag?

查看:119
本文介绍了将类放在脚本标记上有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个代码

<script class="example" type="text/javascript">

并且很好奇,如果有一个好处写入你的代码

and was curious if there is a benefit to writing that into your code

推荐答案

我刚刚对这个标记进行了一个快速测试:

I just ran a quick test with this markup:

<!DOCTYPE html>
<html>
<head>
    <style>
        .foo {
            display: block;
            border: 2px solid red;
            width: 10px;
            height: 10px;
        }
    </style>
</head>
<body>
    <script class="foo" type="text/javascript">
        alert("can you see me?");
    </script>
    after the script  
</body>
</html>

结果是屏幕上显示一个红色块,并且在Chrome中运行时脚本标记的内容可见。 IE不显示脚本内容。因此,< script> 可以像任何其他代码一样处理,至少在Chrome中。我认为这是对Chrome的一部分。这是32位Windows 7上的Chrome 10.0.648.204。

The result was a red block on the screen and the contents of the script tag visible when ran in Chrome. IE does not render the script content visibly at all. So <script> can be treated like any other tag, at least in Chrome. I'd argue that's an oversight on Chrome's part. This is Chrome 10.0.648.204 on 32bit Windows 7.

编辑:Firefox 4也渲染同样的东西。

Firefox 4 also renders the same thing.

EDIT2:可能的用例?使用它作为您的网页上的脚本的显示源,以向人们展示它是如何工作的,也许在一个关于JavaScript的博客上?

Possible use case? Use it as a "show source" for script on your page to show people how it works, perhaps on a blog about JavaScript?

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
</head>
<body>
    <script class="foo" type="text/javascript">
        function foobar() {
            var a = 1;
        }   
    </script>
    after the script
    <a href="#">show me the script</a>

    <script type="text/javascript">
        $('a').click(function(event) {
            event.preventDefault();
            $("<div>").html($(".foo").text()).appendTo($("body"));
        });
    </script>
</body>
</html>

这篇关于将类放在脚本标记上有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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