2013-02-13 42 views
12

Nói rằng tôi có JavaScript sau trong một trang HTMLJavascript biến truy cập trong 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> 

Làm thế nào để có được giá trị của biến "splitText" bên ngoài các thẻ script.

Cảm ơn!

+1

Bạn không nhận được giá trị từ bên ngoài thẻ tập lệnh; thẻ script chèn giá trị vào một nơi khác vào trang. Nhưng để làm điều đó, trước tiên bạn cần phải bọc chức năng của mình để chỉ được gọi khi trang được tải xong. –

+1

Sử dụng giá trị "innerHTML" của phần tử tài liệu ... – Floris

+0

Vâng, về mặt kỹ thuật bạn * có thể * làm điều đó với '', nhưng thường được coi là thực hành không tốt. –

Trả lời

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

Điều này làm việc .... cảm ơn! –

0

Trong javascript liệu, bạn sẽ muốn đặt một id trên thẻ neo của bạn và thực hiện điều này:

<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

Hãy thử điều này:

<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

Nó cũng có thể sử dụng span thẻ thay vì a tag:

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

Nó làm việc tốt cho tôi

Các vấn đề liên quan