29

假设我在 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”的值。

谢谢!

4

6 回答 6

21
<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>
于 2013-02-13T03:04:27.990 回答
3

试试这个 :

<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>
于 2013-02-13T03:06:59.547 回答
2
<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>
于 2013-02-13T03:09:36.857 回答
1

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

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

于 2013-02-13T03:07:49.853 回答
0

在原始 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>
于 2013-02-13T03:05:16.953 回答
0

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 innerHTML built-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.

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>

于 2020-05-27T15:07:11.033 回答