我有一个处理制表符切换的 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 - 在 <script src ="..."></script> 标签中运行 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29414053/