2014-09-23 2 views
1

Я новичок в JavaScript, и вот вызов Я не знаю, как победить!Как временно хранить данные в java-скрипте из тега html?

Вот пример кода, я пробовал:

<body> 
 
    <p id="hello">Hello world.</p> 
 
    
 
    
 
    <script type="text/javascript"> 
 
    
 
    var store,newString,finalString; 
 
    //i want to store the content of <p> tag in store variable 
 
    store=? 
 
    newString="I am Shuvrow"; 
 
    finalString = store + newString; 
 
    //finally i want to replace the content of <p> tag by finalString variable 
 
     
 
    </script> 
 

 
</body>
Благодаря

ответ

2

Используйте атрибут элемента документа innerHTML, как таковой:

<body> 
 
    <p id="hello">Hello world.</p> 
 
    
 
    
 
    <script type="text/javascript"> 
 
    
 
     var store,newString,finalString; 
 

 
     // Get the contents of the element with the ID of 'hello' and assign it to the store variable. 
 
     store = document.getElementById('hello').innerHTML; 
 

 
     newString="I am Shuvrow"; 
 
     finalString = store + newString; 
 

 
     // Replace the contents of the element with the ID of 'hello' with the value of the finalString variable. 
 
     document.getElementById('hello').innerHTML = finalString; 
 
    </script> 
 

 
</body>

Смежные вопросы