JS高手们请进来,javascript添加标签的问题!

在网上搜到一个小程序,很好用,但美中不足的是不能限制添加的数量,哪位高手给改一下,

目的:快速添加标签,但是只能添加5个标签!

高手帮帮忙!

程序:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; cha rset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
a.blue:link,a.blue:visited{
font-size: 12px;
color: #69c;
text-decoration: none;
}
a.blue:hover{
background: #69c;
color: #fff;
}
</style>
<script type="text/javascript">
function fInsert(a,b){
oInput = document.getElementById(a);
oItm = b;
// alert(b.style.backgroundColor);
if((b.style.backgroundColor == "#6699cc")||(b.style.backgroundColor)){
sReplace = oItm.innerHTML.concat(" ");
oInput.value = oInput.value.replace(sReplace,"");
b.style.backgroundColor = "";
b.style.color = "";
}
else{
oInput.value = oInput.value.concat(oItm.innerHTML);
oInput.value = oInput.value.concat(" ");
b.style.backgroundColor = "#69c";
b.style.color = "#fff";
}
}
</script>
</head>
<body>
<input type="text" id="cc" style=" width: 300px;" />
<a href="javascript:void(0 target=_blank);" class="blue" onclick="fInsert(’cc’,this);">关键字</a> <a href="javascript:void(0 target=_blank);" class="blue" onclick="fInsert(’cc’,this);">关键字2</a> <a href="javascript:void(0 target=_blank);" class="blue" onclick="fInsert(’cc’,this);">关键字3</a>
</body>
</html>
高手帮帮忙啊!

第1个回答  推荐于2016-06-04
代码如下,只能输入五个,请运行检测
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; cha rset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
a.blue:link,a.blue:visited{
font-size: 12px;
color: #69c;
text-decoration: none;
}
a.blue:hover{
background: #69c;
color: #fff;
}
</style>
<script type="text/javascript">
var num = 0;
function fInsert(a,b){
oInput = document.getElementById(a);
oItm = b;
// alert(b.style.backgroundColor);
if((b.style.backgroundColor == "#6699cc")||(b.style.backgroundColor)){
sReplace = oItm.innerHTML.concat(" ");
oInput.value = oInput.value.replace(sReplace,"");
b.style.backgroundColor = "";
b.style.color = "";
num--;
}else{
num++;
if(num>5){
num--;
return;
}
oInput.value = oInput.value.concat(oItm.innerHTML);
oInput.value = oInput.value.concat(" ");
b.style.backgroundColor = "#69c";
b.style.color = "#fff";

}
}
</script>
</head>
<body>
<input type="text" id="cc" style=" width: 300px;" />
<a href="javascript:void(0);" class="blue" onclick="fInsert('cc',this);">关键字</a> <a href="javascript:void(0);" class="blue" onclick="fInsert('cc',this);">关键字2</a> <a href="javascript:void(0);" class="blue" onclick="fInsert('cc',this);">关键字3</a> <a href="javascript:void(0);" class="blue" onclick="fInsert('cc',this);">关键字4</a> <a href="javascript:void(0);" class="blue" onclick="fInsert('cc',this);">关键字5</a> <a href="javascript:void(0);" class="blue" onclick="fInsert('cc',this);">关键字6</a>
</body>
</html>本回答被提问者采纳
相似回答