在< script src =" ...">< / script>中运行JavaScript标签? [英] Run JavaScript inside <script src="..."></script> Tags?

查看:173
本文介绍了在< script src =" ...">< / script>中运行JavaScript标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理标签切换的JavaScript文件。这里是源代码:

  var tCount = 0; 

function SwitchToTab(id){
if(id< 0 || id> tCount){id = 0; }($ i $)

for(var i = 0; i document.getElementById(tab+ id).className =active;

for(var i = 0; i< tCount; i ++){document.getElementById(area+ i).style.display =none; }
document.getElementById(area+ id).style.display =;
}

函数InitializeTabs(initialTabId,tabsCount){
tCount = tabsCount;
SwitchToTab(initialTabId);
}

我试图尽可能缩短它的样子:

 < script src =Resources / Tabs.js> InitializeTabs(0,4);< / script> 

它不起作用,但它起作用,如果我将它们分开,如下所示:

 < script src =资源/ Tabs.js>< / script> 
< script> InitializeTabs(0,4);< / script>

那么,有没有办法在< script src = ...>< / script> 标签?我缺少什么?

解决方案

不,这是不可能的。 html规范规定,< script> 标签可以做一个或另一个。

http://www.w3schools.com/tags/tag_script.asprel =nofollow> W3学校,重点是我的。


元素包含脚本语句,它通过src属性指向外部脚本文件。

注意:如果src属性存在,元素必须为空。


< script> Tag Html Spec ,强调我的。


脚本可以在SCRIPT元素的内容或外部文件中定义。如果未设置src属性,则用户代理必须将元素的内容解释为脚本。 如果src具有URI值,则用户代理必须忽略该元素的内容并通过URI检索脚本。



I've a JavaScript file that processes tab switches. Here is the source:

var tCount = 0;

function SwitchToTab(id) {
    if (id < 0 || id > tCount) { id = 0; }

    for (var i = 0; i < tCount; i++) { document.getElementById("tab" + i).className = ""; }
    document.getElementById("tab" + id).className = "active";

    for (var i = 0; i < tCount; i++) { document.getElementById("area" + i).style.display = "none"; }
    document.getElementById("area" + id).style.display = "";
}

function InitializeTabs(initialTabId, tabsCount) {
    tCount = tabsCount;
    SwitchToTab(initialTabId);
}

I'm trying to make it as short as possible like this:

<script src="Resources/Tabs.js">InitializeTabs(0, 4);</script>

It doesn't works but it works if I separate them like this:

<script src="Resources/Tabs.js"></script>
<script>InitializeTabs(0, 4);</script>

So, is there any way to run JavaScript inside <script src="..."></script> tags? What I am missing?

解决方案

No, this is not possible. The html spec dictates that a <script> tag does one or the other.

W3 Schools, emphasis mine.

The element either contains scripting statements, or it points to an external script file through the src attribute.

Note: If the "src" attribute is present, the element must be empty.

<script>Tag Html Spec, emphasis mine.

The script may be defined within the contents of the SCRIPT element or in an external file. If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.

这篇关于在&lt; script src =&quot; ...&quot;&gt;&lt; / script&gt;中运行JavaScript标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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