简体   繁体   English

使用 DOM 事件使文本变为粗体

[英]Making a text bold using DOM Events

i'm learning JavaScript now, and for practicing, i'm trying to make a text editor.我现在正在学习 JavaScript,为了练习,我正在尝试制作一个文本编辑器。 Where you can type something, click a button and make it to upper or lower case, bold and talic.在您可以输入内容的地方,单击一个按钮并将其设置为大写或小写、粗体和斜体。 It worked with lower and upper case, but for some reason it doesn't work with bold.它适用于小写和大写,但由于某种原因它不适用于粗体。

Here's my HTML:这是我的 HTML:

 <h1 id="text">Escreva seu texto abaixo</h1> <div id="container"> <input type="text" id="type"> </div> <main> <ul> <li class="buttons"> <button id="button1">A</button> </li> <li class="buttons"> <button id="button2">a</button> </li> <li class="buttons"> <button id="button3">B</button> </li> <li class="buttons"> <button id="button4">I</button> </li> </ul> </main>

And here's the script:这是脚本:

 > let text = document.getElementById("text") > let input = document.getElementById("type") > let button1 = document.getElementById("button1") > let button2 = document.getElementById("button2") > let button3 = document.getElementById("button3") > let button4 = document.getElementById("button4") > let value1 = document.getElementById("type").value > > > button1.onclick = uppercase > button2.onclick = lowercase > button3.onclick = bold > button4.onclick = italic > > function uppercase(){ > text.innerHTML = value1.toUpperCase() > } > > function lowercase (){ > text.innerText = input.value.toLowerCase() > } > > function bold(){ > text.innerText = input.value.style.fontWeight="bold"; > }

The text that will be changed will be the h1, at the top of the HTML.将要更改的文本将是 h1,位于 HTML 的顶部。 Also, i wrote另外,我写了

let value = document.getElementById("type").value让值 = document.getElementById("type").value

to spare me some time, but when i click the button1 (uppercase) the text just disappear.为了节省我一些时间,但是当我单击 button1(大写)时,文本就消失了。 The button2, with input.value worked fine.带有 input.value 的 button2 工作正常。 So why the var value1 makes and the text disappear and why can't i make the text bold?那么为什么 var value1 使文本消失,为什么我不能使文本变为粗体?

Thank you!`谢谢!`

The problem is you are changing the input.value to bold.问题是您将input.value更改为粗体。 You can't style the input.value you should directly change the input style.您不能设置input.value的样式,您应该直接更改input样式。

Also, you can't use the input.value = it is not built-in function like toUpperCase , it is style.此外,您不能使用input.value =它不像toUpperCase那样内置 function ,它是风格。

Use input.value.style.fontWeight may work, but you need to apply it back which click on other element which is messy.使用input.value.style.fontWeight可能会起作用,但是您需要将其应用回来,然后单击其他杂乱的元素。 The easiest way is to <b> so you don't have to worry to change it back.最简单的方法是<b>这样您就不必担心将其更改回来。

Also, I problem I have to mentioned, you should declare the input value inside the function, you declare it at the beginning which will always be empty since you assign the value at page loading.另外,我必须提到的问题是,您应该在 function 中声明输入值,您在开始时声明它始终为空,因为您在页面加载时分配了值。

 let text = document.getElementById("text") let input = document.getElementById("type") let button1 = document.getElementById("button1") let button2 = document.getElementById("button2") let button3 = document.getElementById("button3") let button4 = document.getElementById("button4") button1.onclick = uppercase button2.onclick = lowercase button3.onclick = bold //button4.onclick = italic function uppercase() { let value1 = document.getElementById("type").value text.innerHTML = value1.toUpperCase() } function lowercase() { text.innerText = input.value.toLowerCase() } function bold() { text.innerHTML = '<b>'+input.value+'</b>' }
 <p id="text">Escreva seu texto abaixo</p> <div id="container"> <input type="text" id="type"> </div> <main> <ul> <li class="buttons"> <button id="button1">A</button> </li> <li class="buttons"> <button id="button2">a</button> </li> <li class="buttons"> <button id="button3">B</button> </li> <li class="buttons"> <button id="button4">I</button> </li> </ul> </main>

you need to give CSS to the object, not the text something like this:您需要将 CSS 提供给 object,而不是像这样的文本:

input.style.fontWeight="bold";

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

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