英文:
I would like to make an input/textarea text bold/italic when i clicked on a button bold/italic
问题
我已经创建了一个文本区域,在这里我会写一些内容。然后我会点击一个按钮。文本将变成粗体。但它不起作用。以下是我的HTML和JS代码:
```HTML:
<textarea cols="30" rows="10" id="textAreaText"></textarea>
<button id="btn-bold">粗体</button>
<button id="btn-italic">斜体</button>
JS:
<script>
document.getElementById('btn-bold').addEventListener('click', function() {
const textArea = document.getElementById('textAreaText');
const textAreaValue = textArea.value;
textArea.style.fontWeight = "bold";
console.log(textAreaValue);
})
</script>
英文:
I have taken a textarea where i will write someting.Then I will click on a button.And the text will turn bold.But it is not working.Here is my html and js code
HTML : <textarea cols="30" rows="10" id="textAreaText"></textarea>
<button id="btn-bold">bold</button>
<button id="btn-italic">italic</button>
js: <script>
document.getElementById('btn-bold').addEventListener('click',function (){
const textArea=document.getElementById('textAreaText');
const textAreaValue=textArea.value;
textAreaValue.style.fontWeight="bold";
console.log(textAreaValue);
})
</script>```
</details>
# 答案1
**得分**: 1
你正在尝试为一个字符串添加样式。
在这里,常量 `textAreaValue` 包含的字符串与 DOM 元素无关,
要使其加粗,请删除以下行:
```javascript
const textAreaValue= textArea.value;
而是应该修改你的 JavaScript 代码如下:
const textArea=document.getElementById('textAreaText');
textArea.style.fontWeight = "bold";
英文:
You're trying to style a string
right here the constant textAreaValue
is holding a string that is not related to the DOM element,
to make it bold remove this line
const textAreaValue= textArea.value;
instead your javascript should look like this
const textArea=document.getElementById('textAreaText');
textArea.style.fontWeight = "bold";
答案2
得分: 0
使文字变为 斜体:
document.getElementById('textAreaText').style['font-style'] = 'italic';
使文字变为 粗体:
document.getElementById('textAreaText').style['font-weight'] = 'bold';
英文:
To make it italic :
document.getElementById('textAreaText').style["font-style"]="italic"
To make it bold :
document.getElementById('textAreaText').style["font-weight"]="bold"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论