2013-02-13 5 views
12

Sagen wir, ich habe folgendes JavaScript in einer HTML-SeiteJavascript Variablenzugriff in HTML

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

Wie kann ich den Wert der Variablen „splitText“ außerhalb der Script-Tags erhalten.

Danke!

+1

Sie erhalten nicht den Wert von außerhalb des Skript-Tags; Das Skript-Tag fügt den Wert an einer anderen Stelle in die Seite ein. Um dies zu tun, müssen Sie zuerst die Funktion so umbrechen, dass sie nur aufgerufen wird, wenn die Seite geladen wurde. –

+1

Verwenden Sie den "innerHTML" -Wert des Dokumentelements ... – Floris

+0

Nun, technisch könnten Sie es mit '' machen, aber es wird allgemein als schlechte Praxis angesehen. –

Antwort

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

Das hat funktioniert .... danke! –

0

In raw Javascript, sollten Sie eine ID auf dem Anker-Tag legen und dies zu tun:

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

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

Es ist auch möglich span-Tag statt a Tag zu verwenden:

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

Es funktionierte gut für mich