2017-05-24 100 views
0

相当新的JavaScript和希望我来对地方。Javascript - .innerHTML - 正确显示文本格式

目前我正在尝试制作一个按钮,将特定格式的文本放置在文本框中,但我不确定如何使文本保持其格式。

我希望能够在文本中包含像p/p和b/b这样的元素。

例子我怎么想看到它:
客户有问题,
请前往的地点和修理服务

的Javascript:

function myFunction() { 
document.getElementById("textbox").innerHTML = "Customer is having problems, please proceed to the location and repair the service "; 
    var oTextbox1 = document.getElementById("textbox"); 
oTextbox1.focus(); 
oTextbox1.select(); 
document.execCommand('copy') 
} 

任何帮助将是不胜感激。

回答

2

If you use innerHTML then html gets rendered, if you use innerText then it is left unrendered

Considering input can't contain HTML你可能想看看contenteditableRelated question

document.getElementById("test1").innerHTML = "<b>Bolded?</b>"; 
 
document.getElementById("test2").innerText = "<b>Bolded?</b>";
<div id="test1" contenteditable></div> 
 
<div id="test2" contenteditable></div>

+0

谢谢你,但它不似乎为我工作,我的按钮直接放置到一个文本框不开放股利。这可能是问题吗? –

+0

document.getElementById(“textbox”)。innerHTML =“客户有问题,
请前往位置并修复服务”;在该字符串中使用html,你不能。 –

+2

[你不能把HTML放在输入字段中](https://stackoverflow.com/questions/5823835/html-in-the-input-field-value) – Hodrobond