javascript - 在 <script src ="..."></script> 标签中运行 JavaScript?

标签 javascript html

我有一个处理制表符切换的 JavaScript 文件。这是来源:

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);
}

我尽量让它尽可能短,如下所示:

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

它不起作用,但如果我像这样分开它们,它就会起作用:

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

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

最佳答案

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

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

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.

关于javascript - 在 &lt;script src ="...">&lt;/script&gt; 标签中运行 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29414053/

相关文章:

javascript - 在 javascript/momentjs 中创建日期时忽略时区

javascript - 注册时检查用户名唯一性

html - 图像未从 CSS 加载

html - 如何在不使用绝对位置的情况下使我的 h1 位于图像背景的中心?

html - 没有重叠的固定导航 div(无高度)

html - Youtube iframe自动播放功能不适用于Chromium,但适用于Manjaro Linux上的Firefox

javascript - Typescript - 如何将部分类型分配给另一个对象的相同类型的属性?

javascript - 两级列表的 HTML 组件

javascript - 'this' 在 function() 中未定义。 Angular 2

c# - 如何从 HttpListener 提供相关文件