简体   繁体   English

打破onClick(HTML)内部的界限

[英]break the line inside onClick (HTML)

I want to break the line after the appending of code. 我想在添加代码后换行。 Example: 例:

string1= abc
string2= 123    

I am geting the appending as abc 123. But i want it as 我得到的附加为abc123。但是我希望它为

abc abc

123 123

  <input name="string1" >    
 <input name="string2" ><p>    
 <input name="showMeArea" readonly="true"><br>    
 <input type="button" value="Combine Strings"    
 OnClick="showMeArea.value= string1.value + ' ' +string2.value;"></p>        

To insert a line-break in Javascript simply insert \\n 要在Java语言中插入换行符,只需插入\\ n

showMeArea.value= string1.value + '\\n' +string2.value; showMeArea.value = string1.value +'\\ n'+ string2.value;

just add new line sign \\n 只需添加新行号\\ n

OnClick="showMeArea.value= string1.value + '\n' +string2.value;

also note: you should use textarea. 另请注意:您应该使用textarea。 input field cannot display multiline text 输入字段无法显示多行文字

You can either use an escape, such as \\n or you can just use <br> . 您可以使用转义符,例如\\n ,也可以只使用<br> The br is a line break within HTML and \\n and within Javascript. br是HTML和\\ n以及Javascript中的换行符。

You cannot insert a line break into the value of an <input> element with type=text (the default), since it is by definition for a single line of input. 您不能将换行符插入具有type=text (默认值)的<input>元素的值中,因为按定义它是针对单行输入的。 If you try to insert \\n there, browsers strip it off. 如果尝试在其中插入\\n ,浏览器会将其删除。 You need to use eg a textarea element instead. 您需要使用例如textarea元素代替。 Moreover, your code that refers to elements by using their name attribute values as variable identifiers is obsolete and does not work in many modern browsers. 此外,通过将元素的name属性值用作变量标识符来引用元素的代码已过时,并且在许多现代浏览器中均无法使用。

The following works: 以下作品:

 <input name="string1" id="string1"> <input name="string2" id="string2"><p> <textarea name="showMeArea" id="showMeArea" readonly="true"></textarea><br> <input type="button" value="Combine Strings" OnClick="document.getElementById('showMeArea').value = document.getElementById('string1').value + '\\n' + document.getElementById('string2').value;"></p> 

you can use 您可以使用

<script>
function combine() {
   var s1 = document.getElementById("string1").value
   var s2 = document.getElementById("string2").value
   document.getElementById("showMeArea").value = s1 +"\n\n" + s2
   document.getElementById("showMeAreaHtml").innerHTML = s1 +"<br><br>" + s2
}
</script> 
<input name="string1" id="string1">     
<input name="string2" id="string2">
<input type="button" value="Combine Strings" onclick="combine()">
<br>
<textarea name="showMeArea" id="showMeArea" readonly="true" rows="5" cols="20"></textarea>
<p id="showMeAreaHtml"></p>

Note: use document.getElementById is more cross browser then only name.value; 注意:使用document.getElementById是跨浏览器,然后才是name.value;。 use .value for input and textarea, .innerHTML for other html elements 使用.value作为输入和文本区域,使用.innerHTML作为其他html元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM