HTML 中的 Javascript 变量访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14845710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:07:15  来源:igfitidea点击:

Javascript variable access in HTML

javascript

提问by Damien-Amen

Say I have the following JavaScript in a HTML page

假设我在 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>

How do I get the value of the variable "splitText" outside the script tags.

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

Thanks!

谢谢!

采纳答案by kufudo

<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>

回答by Iswanto San

Try this :

尝试这个 :

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
            var simpleText = "hello_world";
            var finalSplitText = simpleText.split("_");
            var splitText = finalSplitText[0];
            $("#target").text(splitText);
        });
</script>

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

回答by Floris

<html>
<head>
<script>
    function putText() {
        var simpleText = "hello_world";
        var finalSplitText = simpleText.split("_");
        var splitText = finalSplitText[0];
        document.getElementById("destination").innerHTML = "I need the value of " + splitText + " variable here";
    }
</script>
</head>
<body onLoad = putText()>
    <a id="destination" href = test.html>I need the value of "splitText" variable here</a>
</body>
</html>

回答by praneetloke

Here you go: http://codepen.io/anon/pen/cKflA

给你:http: //codepen.io/anon/pen/cKflA

Although, I must say that what you are asking to do is not a good way to do it. A good way is this: http://codepen.io/anon/pen/jlkvJ

虽然,我必须说你要求做的不是一个好方法。一个好方法是:http: //codepen.io/anon/pen/jlkvJ

回答by cliffbarnes

In raw javascript, you'll want to put an id on your anchor tag and do this:

在原始 javascript 中,您需要在锚标记上放置一个 id 并执行以下操作:

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

function insertText(){
    document.getElementById('someId').InnerHTML = splitText;}
</script>

<body onload="insertText()">
<a href = test.html id="someId">I need the value of "splitText" variable here</a>
</body>
</html>

回答by Peyman Kheiri

It is also possible to use spantag instead of atag:

也可以使用spantag 代替atag:

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

   document.getElementById('someId').InnerHTML = splitText;
</script>

 <body>
  <span id="someId"></span>
 </body>

</html>

It worked well for me

它对我很有效

回答by James B

The info inside the <script>tag is then processed inside it to access other parts. If you want to change the text inside another paragraph, then first give the paragraph an id, then set a variable to it using getElementById([id])to access it ([id] means the id you gave the paragraph). Next, use the innerHTMLbuilt-in variable with whatever your variable was called and a '.' (dot) to show that it is based on the paragraph. You can set it to whatever you want, but be aware that to set a paragraph to a tag (<...>), then you have to still put it in speech marks.

然后在<script>标签内部处理信息以访问其他部分。如果您想更改另一个段落中的文本,则首先为该段落指定一个 id,然后使用它设置一个变量getElementById([id])来访问它([id] 表示您为该段落提供的 id)。接下来,将innerHTML内置变量与调用的任何变量和一个 ' 一起使用'(点)表示它基于段落。您可以将其设置为任何您想要的值,但请注意,要将段落设置为标签 (<...>),您仍必须将其放入语音标记中。

Example:

例子:

<!DOCTYPE html>
<html>
    <body>
        <!--\|/id here-->
        <p id="myText"></p>
        <p id="myTextTag"></p>
        <script>
            <!--Here we retrieve the text and show what we want to write...
            var text = document.getElementById("myText");
            var tag = document.getElementById("myTextTag");
            var toWrite = "Hello"
            var toWriteTag = "<a href='https://stackoverflow.com'>Stack Overflow</a>"
            <!--...and here we are actually affecting the text.-->
            text.innerHTML = toWrite
            tag.innerHTML = toWriteTag
        </script>
    <body>
<html>