2015-05-01 43 views
0

當談到javascript時,我是一個總的新手。我有一個文本框和一個與之關聯的按鈕。當我運行onclick函數時,我想從文本框中獲取信息並將其移動到:如何從文本框中獲取信息並將其移入段落元素?

「當使用onclick函數單擊提交按鈕時,文本將從文本框移動到此處

我已經嘗試了很多不同的東西,但至今沒有運氣。 感謝您的幫助。

乾杯!

+0

歡迎來到Stack Overflow!請包括您迄今爲止嘗試的內容。 –

+0

網上有很多資源。我建議你參考[其中之一](http://www.w3schools.com/html/)。 –

回答

1

如果你想使用jQuery,這裏有一個簡單的解決方案。

/** 
 
    * Using jQuery, grab the <pre> element on the page, and fill 
 
    * them with the text within the textarea. 
 
    */ 
 

 
    function moveText() { 
 
     "use strict"; 
 
     $("pre").html($("textarea").val()); 
 
    } 
 

 
    /** 
 
    * Using pure javascript, do exactly the same thing as moveText(); 
 
    */ 
 

 
    function pure_js() { 
 
     "use strict" 
 
     document.getElementsByTagName("pre")[0].innerHTML = document.getElementsByTagName("textarea")[0].innerHTML; 
 
    }
<html> 
 

 
<head> 
 
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> 
 
</head> 
 

 
<body> 
 
    <textarea>Default text.</textarea> 
 
    <button type="button" onClick="pure_js()">Move the Text</button> 
 
    <pre></pre> 
 
</body> 
 

 
</html>

+0

有沒有辦法做到這一點,而不使用jQuery。由於我是新手,我還沒有機會看看jQuery。謝謝。 –

+0

當然。我編輯我的帖子上面,包括一個純JS版本做同樣的事情。 _注意按鈕元素的onClick調用中的函數名稱._ – kneeki

相關問題