我在使用DIV標記和表單輸入字段時遇到了一些問題。我使用javascript獲得輸入字段的值。 Javascript工作正常。它獲取輸入字段值並顯示在DIV標籤中。問題是,當我輸入一個長輸入或長句時,它顯示在div標籤中。我希望它不是以很長的單行顯示,而是以段落的形式顯示。這是我的代碼。div標記和輸入標記的換行調整
<script>
function myFunc(){
var str = document.myform.aaa.value;
document.getElementById('mydiv').innerHTML=str;
}
</script>
<form>
<input type="text" name="aaa" />
<input type="button" value="Post" onclick="myFunc();" >
</form>
<div id="mydiv" width="500px" height="300px">Old text</div>
如果我給它一個例子的輸入:
hello there, how are you hello there, how are youhello there, how are youhello there.
它顯示在一個單一的行,而不是段落格式。我希望它以這種形式顯示上面的句子:
hello there, how are you hello there,
how are youhello there, how are youhello
there.
我有點自己梳理一下。我的意思是說,如果我用空格寫一個句子,它可以正常工作,但如果有一長串沒有空格的字母,那麼它就不起作用。我想要的是,無論我是否可以鍵入空格而沒有空格,都應該雙向工作。
所以給div設置一個寬度。 – j08691
@ j08691 ...我將它的寬度設置爲500px,高度爲300px。仍然不工作 – Rebbeca
也許你的寬度太寬。在這裏工作很好http://jsfiddle.net/j08691/yAwrH/1/ – j08691