2016-11-07 51 views
0

我有一个简单的textarea的一个按钮:jQuery TextArea Bug?

我改变textarea的与

$("button.test").click(function(){ 
    $("#config_oh").contentEditable = true; 
$("#config_oh").html("Hello World); 

}); 

这工作的内容。但是,如果我在textarea中输入内容并重新运行脚本,它将不起作用:内容在那里(我可以通过Chrome检查器看到它),但网页上没有显示任何内容。为什么?

如果我使用val()更新textarea的内容,它将起作用,但不会执行HTML呈现,例如,我看到html特殊字符。

+0

请检查此链接:http://stackoverflow.com/questions/4705848/rendering-html-inside-textarea – Gowri

+0

我做它的代码:是那不正确? – giuseppe

回答

0

个人代替

$("#config_oh").html("Hello World);

,我建议做的是定制的div与conteneditable = true,如在意见提出。这样,.html()方法照常工作。

<style> 
     .editable { 
      width: 100%; 
      height: 200px; 
      border: 1px solid #ccc; 
      padding: 5px; 
      resize: both; 
      overflow: auto; 
     } 

     </style> 
<button class="test">Click</button> 
<div class="editable" contenteditable="true" id="config_oh"> MY TEXT HERE </div> 

<script> 
$("button.test").click(function(){ 
    $("#config_oh").html("Hello World); 
}); 
</script> 

通过这种方式,即使在“textarea”中键入内容,按钮单击按预期工作。

0

$("#config_oh").val("Hello World");

+0

我知道,但1)为什么这种行为; 2)val没有被渲染.... html特殊字符正在显示 – giuseppe

+0

通过手动键入或使用预设值(如我们在本例中),您放入textarea的任何值将以明文形式呈现。 –

+0

如果您想在页面上呈现html,最好的办法是将contentEditable标签添加到div元素上。类似于[this](http://jsbin.com/oBiBoBE/10/edit?html,output)的东西就可以做到。 –

0

您必须在输入框中设置文本。你使用val()。 输入类型有一个属性。 如:

$("#config_oh").val("Hello World"); 

这应该是工作