简体   繁体   English

将文本区域中的少量文本更改为粗体

[英]Changing a small amount of text in a textarea to bold

Say I have this text说我有这个文本

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit。

and I want to make the bold我想让粗体

Lorem ipsum dolor sit amet , consectetur adipiscing elit. Lorem ipsum dolor sit amet , consectetur adipiscing elit。

If I was to do this in Javascript如果我要在 Javascript 中执行此操作

 function myFunction(obj) { var text1 = "Lorem ipsum "; var text2 = "dolor sit amet"; var text3 = ", consectetur adipiscing elit."; text2.bold(); obj.value = text1 + text2 + text3; }
 <textArea onmouseup=myFunction(this)>Lorem ipsum dolor sit amet, consectetur adipiscing elit</textArea>

I can't seem to bold the text, any ideas?我似乎无法加粗文字,有什么想法吗?

As you can't style text inside a textarea ,由于您无法在textarea内设置文本样式,
Here is a solution using a content-editable div instead:这是一个使用内容可编辑div的解决方案:
(Inspired by my solution to another question: Change font for selected text using JavaScript ) (受我对另一个问题的解决方案的启发: Change font for selected text using JavaScript

 function changeStyle(style) { var sel = window.getSelection(); // Gets selection if (sel.rangeCount) { // Creates a new element, and insert the selected text with the chosen style var e = document.createElement('span'); e.classList.add(style.value); // Selected style (class) e.innerHTML = sel.toString(); // Selected text // https://developer.mozilla.org/en-US/docs/Web/API/Selection/getRangeAt var range = sel.getRangeAt(0); range.deleteContents(); // Deletes selected text… range.insertNode(e); // … and inserts the new element at its place } }
 .editable { width: 360px; height: 120px; border: 1px solid #ccc }.editable.span-0{ /* Added to reset styles correctly */ font-weight: normal; /* Reset b */ text-decoration: none; /* Reset u */ font-style: normal; /* Reset i */ }.editable.span-b{ font-weight: bold; }.editable.span-u{ text-decoration: underline; }.editable.span-i{ font-style: italic; }
 Highlight text and change style<br> <select id="select_font" onchange="changeStyle(this);"> <option value="span-0" selected>None</option> <option value="span-b">Bold</option> <option value="span-u">Underlined</option> <option value="span-i">Italic</option> </select> <div contenteditable="true" class="editable"> Some Content </div>

(I added other styling options… just because!) (我添加了其他样式选项……只是因为!)

I hope it helps!我希望它有帮助!

The result should be stored like this:结果应该这样存储:

function myFunction(obj) {;
  var text1 = "Lorem ipsum ";
  var text2 = "dolor sit amet"
  var text3 = ", consectetur adipiscing elit.";
  text2 = text2.bold();

  obj.value = text1 + text2 + text3;
}

More info: https://www.w3schools.com/jsref/jsref_bold.asp更多信息: https://www.w3schools.com/jsref/jsref_bold.asp

As far as I know, we can't style textarea and don't accept any HTML.据我所知,我们不能设置 textarea 样式,也不接受任何 HTML。

text2 = text2.bold() text2 = text2.bold()

will return dolor sit amet将返回dolor sat amet

but it will appear as text with b-tags.但它会显示为带有 b 标记的文本。

For divs it would be done like this, however you can not include a span in a textArea, so if the textArea is necessary i cant really help对于 div,它会像这样完成,但是你不能在 textArea 中包含跨度,所以如果 textArea 是必要的,我真的帮不上忙

 function myFunction(obj) {; document.getElementById("span").style.fontWeight="bold"; }
 <div onmouseup=myFunction(this) >Lorem ipsum <span id="span">dolor sit amet, </span> consectetur adipiscing elit</div>

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

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