简体   繁体   English

用户单击按钮后如何使文本变为粗体?

[英]How do I make text bold after the user clicks a button?

I am trying to make a text editor in JavaScript and it has various features like bolding the text and italics and some others.我正在尝试用 JavaScript 制作一个文本编辑器,它具有各种功能,例如加粗文本和斜体以及其他一些功能。 When the user clicks on any one of them, then anything that they have selected will become edited accordingly.当用户点击其中任何一个时,他们选择的任何内容都将相应地进行编辑。 Now I am able to get the selected text and even put strong tags before and after the text but when I do it the text doesn't get bold only the tags are seen.现在我能够获取选定的文本,甚至在文本之前和之后放置强标签,但是当我这样做时,文本不会变粗,只会看到标签。 How can make the text bold?如何使文本加粗? I sorry but I am unable to provide code since its too much and too messy.对不起,我无法提供代码,因为它太多太乱了。 Thanks for reading query!感谢阅读查询! Also if you know any JavaScript library which could help me solve this problem then you can suggest me.另外,如果您知道任何可以帮助我解决此问题的 JavaScript 库,那么您可以向我推荐。

 document.getElementById("button").addEventListener("click", () => { let text = document.getElementById("text").innerText let selection = window.getSelection(); let boldText = "<strong>" + selection + "</strong>" document.getElementById("text").innerText = text.replace(selection, boldText) })
 <div id="text"> The dog barks and runs around. </div> <button id="button">Make selected text bold</button>

I am using inner text instead of inner html in order to reproduce the problem我正在使用内部文本而不是内部 html 来重现问题

You are using innerText to replace which is wrong as we want to change html so use innerHTML .您正在使用innerText替换哪个是错误的,因为我们要更改 html 所以使用innerHTML Code:代码:

 document.getElementById("button").addEventListener("click", () => { let text = document.getElementById("text").innerText let selection = window.getSelection(); let boldText = "<b>" + selection + "</b>" document.getElementById("text").innerHTML = text.replace(selection, boldText) //use innerhtml instead of innertext })
 <div id="text"> The dog barks and runs around. </div> <button id="button">Make selected text bold</button>

Here is some code that I used to change to inputted text on a button click这是我用来在单击按钮时更改为输入文本的一些代码

<input type="text" id="sign" value="">
<p style = "font-family: 'Dancing Script', cursive; font-size:50px;" 
id="signOutput"></p>
<button onclick="signFunction()">Output Signature</button>

<script>

function signFunction() {var x = document.getElementById("sign").value;
         document.getElementById("signOutput").innerHTML = x;
         }

</script>
<p id="mypar">Hello</p>
<br>
<button type="button" onclick="changeStyle()">Click Me</button>
<script>
function changeStyle(){
    var element = document.input
    var element = document.getElementById("mypar");
    element.style.fontWeight = "bold";
}
</script>

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

相关问题 单击一定数量后如何使按钮消失 - How do I make a button disappear after a certain amount of clicks 在单击警报后删除所述按钮之前,如何强制将按钮文本更改为粗体? - How do I force to change the button text to bold before removing the said button after an alert on click? 当用户单击菜单按钮时,如何弹出菜单? - How do I make a menu pop up when the user clicks the menu button? 当用户单击按钮时,如何保存可重复使用的React组件? - How do i make my reusable react component saves when the user clicks button? 当用户点击按钮时,如何让html运行javascript函数? - How do I make html run a javascript function when a user clicks a button? 用户单击文件上传中的关闭按钮后,如何关闭窗口? - How do I close a window after the user clicks on the close button in file upload? 如何在对象数组中使文本变为粗体? - How do I make text bold in an array of objects? 当用户单击按钮时如何防止作弊 - How do I prevent cheating when user clicks a button 我如何创建一个(多个)弹出文本框,该文本框会在鼠标悬停时显示,并且如果用户单击“ +”号 - How do I make create a (more than one) popup text box that appears on mouse-over AND if user clicks on “+” sign 如何在javascript中使单词变粗? - How do I make a word bold in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM