gpt4 book ai didi

HTML 中的 Javascript 变量访问

转载 作者:可可西里 更新时间:2023-11-01 01:17:56 25 4
gpt4 key购买 nike

假设我在 HTML 页面中有以下 JavaScript

<html>
<script>
var simpleText = "hello_world";
var finalSplitText = simpleText.split("_");
var splitText = finalSplitText[0];
</script>

<body>
<a href = test.html>I need the value of "splitText" variable here</a>
</body>
</html>

如何获取脚本标签外的变量“splitText”的值。

谢谢!

最佳答案

<html>
<script>
var simpleText = "hello_world";
var finalSplitText = simpleText.split("_");
var splitText = finalSplitText[0];

window.onload = function() {
//when the document is finished loading, replace everything
//between the <a ...> </a> tags with the value of splitText
document.getElementById("myLink").innerHTML=splitText;
}

</script>

<body>
<a id="myLink" href = test.html></a>
</body>
</html>

关于HTML 中的 Javascript 变量访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14845710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com