HTML中的JavaScript变量访问

前端之家收集整理的这篇文章主要介绍了HTML中的JavaScript变量访问前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
说我在 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>
原文链接:https://www.f2er.com/html/225061.html

猜你在找的HTML相关文章