如下代码,怎样使用onclick单击事件实现点击一次变成一个状态再点击一次变回来?
代码如下:<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>work...
代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>work3.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<style type="text/css">
.boldStyle{
font-weight: bold;
color: black;
}
#div1{
border: 1px solid black;
width: 35px;
padding-left: 5px;
background: #1E90FF;
}
</style>
<script type="text/javascript">
function changeBold(obj,flag){
var obj = document.getElementById('first');
obj.className="boldStyle";
}
</script>
</head>
<body>
<textarea rows="5" cols="20" id="first"></textarea>
<div id="div1" onclick="changeBold()">Bold</div>
</body>
</html>
JS中要怎么写才能完成点击Bold字体变粗再点一次Bold字体变回之前的状态? 展开
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>work3.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<style type="text/css">
.boldStyle{
font-weight: bold;
color: black;
}
#div1{
border: 1px solid black;
width: 35px;
padding-left: 5px;
background: #1E90FF;
}
</style>
<script type="text/javascript">
function changeBold(obj,flag){
var obj = document.getElementById('first');
obj.className="boldStyle";
}
</script>
</head>
<body>
<textarea rows="5" cols="20" id="first"></textarea>
<div id="div1" onclick="changeBold()">Bold</div>
</body>
</html>
JS中要怎么写才能完成点击Bold字体变粗再点一次Bold字体变回之前的状态? 展开
3个回答
展开全部
在原来的基础上稍微修改下就可以了,思想是在点击的时候判断obj.className是否是加粗,如果是加粗就改变成正常的
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>work3.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<style type="text/css">
.boldStyle{
font-weight: bold;
color: black;
}
.unbold{
font-weight: normal;
}
#div1{
border: 1px solid black;
width: 35px;
padding-left: 5px;
background: #1E90FF;
}
</style>
<script type="text/javascript">
function changeBold(obj,flag){
var obj = document.getElementById('first');
var pre = obj.className;
if(pre=="boldStyle"){
obj.className="unbold";
}else{
obj.className="boldStyle";
}
}
</script>
</head>
<body>
<textarea rows="5" cols="20" id="first"></textarea>
<div id="div1" onclick="changeBold()">Bold</div>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var status = 0;
function changeBold(obj,flag){
var obj = document.getElementById('first');
obj.className= status == 0 ? "boldStyle" : "";
if (status) {
status = 0;
} else {
status = 1;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改下js
var flag;
function changeBold(){
var obj = document.getElementById('first');
if(flag){
obj.className="boldStyle";
} else {
obj.className="";
}
flag = !flag;
}
var flag;
function changeBold(){
var obj = document.getElementById('first');
if(flag){
obj.className="boldStyle";
} else {
obj.className="";
}
flag = !flag;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询