2013-02-13 12 views
12

Say mam następujący JavaScript w stronie HTMLJavaScript zmienna dostęp 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> 

Jak mogę uzyskać wartość zmiennej „splitText” poza znaczniki skryptów.

Dzięki!

+1

Nie dostaniesz wartość z poza tagiem skryptu; znacznik script wstrzykuje wartość gdzie indziej na stronę. Ale aby to zrobić, najpierw musisz zawinąć funkcję, aby wywołać ją dopiero po załadowaniu strony. –

+1

Użyj wartości "innerHTML" elementu dokumentu ... – Floris

+0

Cóż, technicznie możesz * możesz * zrobić to z '', ale ogólnie uważa się, że jest to zła praktyka. –

Odpowiedz

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

To zadziałało .... dzięki! –

0

surowca javascript, będziemy chcieli, aby umieścić identyfikator na tagu zakotwiczenia i zrobić to:

<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

Spróbuj tego:

<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

Możliwe jest również użycie span tag zamiast a tagu:

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

to działa dobrze dla mnie