![](/img/trans.png)
[英]How can I move a variable over to another html file and change the text on the html file based on the variable
[英]How can I change text in an html file using a javascript variable?
我有一個 html 文件“lobby.html”和一個 js 文件“lobby.js”
如果這是在大廳。html,
<body>
<span id="lobbyCode"></span>
</body>
這是在 lobby.js 中,
var lobbyCode = "abcdefg";
如何使 html 文件顯示“abcdefg”?
在您的 html 文件中,您需要導入外部 js 文件。
在結束正文標記上方寫下以下行,以確保它可以看到所有 html 元素。
注意:考慮到 src 屬性值處的文件路徑取決於您的文件結構。
<script src="lobby.js"></script>
</body>
你也可以把它放在你的 header
<head>
<script src="lobby.js"></script>
</head>
然后在您的外部 js 文件中,您現在可以使用 document.getElementById 選擇器訪問 html 文件內部,您可以通過將 innerHTML 屬性設置為變量的值來更新其值
外部 JS 文件
var lobbyCode = "abcdefg";
document.getElementById("lobbyCode").innerHTML = lobbyCode;
var lobbyCode = "abcdefg"; document.getElementById("lobbyCode").innerHTML = lobbyCode;
<body> <span id="lobbyCode"></span> </body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.