简体   繁体   English

在文本区域中将文本设置为粗体

[英]Setting a text to Bold in a textarea

Heres the particular html code, which consists of the button 'bold' and the text area. 这是特定的html代码,它由按钮“粗体”和文本区域组成。

<form name="myform">
 < input type="button" onClick="Bold()" value="Bold"> 
</form>

<textarea name="myTextArea" id="myTextArea" cols="100" rows="14" placeholder="Enter Text Here ...">< /textarea>

Heres My Javascript 这是我的Java语言

function Bold() {
    myTextArea.document.execCommand('bold',false,null); 
}

What am i doing wrong ???? 我究竟做错了什么 ????

If you want the textarea to display bold text, you can do that via css style attribute: 如果希望textarea显示粗体文本,则可以通过css style属性来实现:

<textarea style="font-weight: bold">test-text</textarea>

Also, you can use div insted, and set contenteditable="true" http://jsfiddle.net/XNkDx/2852/ And, use hotkey ctrl+b 另外,您可以使用div insted,并设置contenteditable="true" http://jsfiddle.net/XNkDx/2852/并且,使用热键ctrl+b

UPDATE: If you want to use button, just, get selected text and change it http://jsfiddle.net/XNkDx/2859/ 更新:如果您只想使用按钮,请获取选定的文本并将其更改为http://jsfiddle.net/XNkDx/2859/

You can simply use javascript like this: 您可以像这样简单地使用javascript:

function Bold() {
    document.getElementById("myTextArea").style.fontWeight = 'bold'; 
}

Demo 演示

I Think you should use string.bold() function of javascript like this one: 我认为您应该像这样使用javascript的string.bold()函数:

function Bold() {
    var str = document.getElementById('myTextArea').value();
    var result = str.bold();
}

or you can use css style 或者您可以使用CSS样式

{font-weight:bold;}

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

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