[英]Button that makes textarea text bold
I have a button that has an onclick="bold()"
and it is supposed to be changing the font weight from normal to bold and then back to normal. 我有一个具有onclick="bold()"
的按钮,它应该将字体的粗细从正常更改为粗体,然后再恢复为正常。 It isn't doing anything though. 它什么也没做。 What do I need to change or add? 我需要更改或添加什么? Here is a jsfiddle if that helps: http://jsfiddle.net/dZVfh/ 如果有帮助,请使用jsfiddle: http : //jsfiddle.net/dZVfh/
<script>
var x = document.getElementById('description-box');
function bold() {
if( $(x).css("font-weight") == "normal") {
$(x).css("font-weight"), "bold");
}
else {
$(x).css("font-weight", "normal");
}
}
</script>
There is a syntax error in your code: 您的代码中存在语法错误:
Change 更改
$(x).css("font-weight"), "bold");
to 至
$(x).css("font-weight", "bold");
Full function 功能齐全
function bold() {
var x = $('#description-box');
if (x.css("font-weight") !== "bold") {
x.css("font-weight", "bold");
} else {
x.css("font-weight", "normal");
}
}
Working fiddle : http://jsfiddle.net/dZVfh/3/ 工作提琴 : http : //jsfiddle.net/dZVfh/3/
There were a number of problems in your original fiddle. 您原来的小提琴有很多问题。
jQuery was not being included jQuery未包含在内
The function was defined inside domReady
- which means it isn't visible when clicking the button 该函数在domReady
中定义-这意味着单击按钮时不可见
The CSS block had a syntax error. CSS块存在语法错误。 The style was being closed with a )
instead of a }
样式用)
而不是}
封闭
You were trying to assign the element to x
before the DOM was ready. 您试图在DOM准备就绪之前将元素分配给x
。 The new fiddle does this inside the function (by which time the DOM is ready) 新的小提琴在函数内部执行此操作(到DOM准备就绪的时间)
i prefer to have it change class : 我更喜欢让它改变班级:
add this style 添加此样式
.bold{
font-weight: bold;
}
change your function : 改变你的功能:
function bold(){
if( $(x).hasClass("bold")) {
$(x).removeClass("bold");
}
else {
$(x).addClass("bold");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.