2013-02-13 12 views
12

में चर का उपयोग मैं एक 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> 

मैं चर "splitText" स्क्रिप्ट टैग के बाहर का मान कैसे प्राप्त करूं में निम्न JavaScript है कहो।

धन्यवाद!

+1

आप से मूल्य नहीं मिलता है स्क्रिप्ट टैग के बाहर; स्क्रिप्ट टैग पेज में कहीं और मूल्य इंजेक्ट करता है। लेकिन ऐसा करने के लिए, आपको सबसे पहले अपने फ़ंक्शन को केवल तभी कॉल करने की आवश्यकता होती है जब पृष्ठ लोड हो रहा हो। –

+1

दस्तावेज़ तत्व के "आंतरिक HTML" मान का उपयोग करें ... – Floris

+0

ठीक है, तकनीकी रूप से आप * 'के साथ कर सकते हैं, लेकिन इसे आमतौर पर खराब अभ्यास माना जाता है। –

उत्तर

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

यह काम किया .... धन्यवाद! –

0

कच्चे जावास्क्रिप्ट में, आप अपने एंकर टैग पर एक आईडी रखना चाहते हैं और ऐसा करते होगी:

<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

इस प्रयास करें:

<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

यह भी a टैग के बजाय span टैग का उपयोग करना संभव है:

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

यह मेरे लिए अच्छी तरह से काम

संबंधित मुद्दे