Javascript variable access in HTML

后端 未结 7 1315
余生分开走
余生分开走 2020-12-13 09:44

Say I have the following JavaScript in a HTML page




        
相关标签:
7条回答
  • 2020-12-13 10:25

    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>
    
    0 讨论(0)
  • 2020-12-13 10:25

    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>

    0 讨论(0)
  • 2020-12-13 10:27

    Here you go: 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

    0 讨论(0)
  • 2020-12-13 10:33
    <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>
    
    0 讨论(0)
  • 2020-12-13 10:33

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

    <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>
    
    0 讨论(0)
  • 2020-12-13 10:37
    <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>
    
    0 讨论(0)
提交回复
热议问题