能不能用js控制input的class属性

paizxin 2008-06-04 09:21:08
我想用js改input的class属性,能做到这个功能吗?

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "text" || inputs[i].type == "password") {
"这里应该怎么写?"
}
}
...全文
772 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
JunKi108 2008-06-05
  • 打赏
  • 举报
回复
inputs[i].className == "你所定义的CSS";
浴火_凤凰 2008-06-05
  • 打赏
  • 举报
回复
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "text" || inputs[i].type == "password") {
"这里应该怎么写?"//inputs[i].className ="";
}
}
yzsunlight 2008-06-05
  • 打赏
  • 举报
回复
<!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; charset=gb2312" />
<title>无标题文档</title>

<script type="text/javascript">
function xy(){
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "text" || inputs[i].type == "password") {
inputs[i].className = "dd";
}
}
}

function cc(){
alert('aaa');
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "text" || inputs[i].type == "password") {
inputs[i].className = "cc";
}
}
}
</script>
<style type="text/css">
<!--
.dd {
font-family: "宋体";
border:red;
}

.cc{
border:blue;
}
-->
</style>
</head>

<body onload="xy()">
<input type="text" name="t" id="t" />
<input type="button" name="f" onclick="cc()" />
</body>
</html>
xiaoqiangcn 2008-06-04
  • 打赏
  • 举报
回复
inputs[i].className = '';
inputs[i].style.backgroundColor = 'red';
都可以,你自己选择的用
lansb1 2008-06-04
  • 打赏
  • 举报
回复
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "text" || inputs[i].type == "password") {
inputs[i].style.backgroundColor = 'red';
}
}
yu__yfchun125 2008-06-04
  • 打赏
  • 举报
回复
textfeild.className= "你的样式名"
familyX 2008-06-04
  • 打赏
  • 举报
回复
我给你的例子就是的啊。可以的。你可以再加一个默认的,然后用我的那个改就可以了。
jackyBody 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 paizxin 的帖子:]
我想用js改input的class属性,能做到这个功能吗?

JScript code
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "text" || inputs[i].type == "password") {
"这里应该怎么写?"//inputs[i].className = ''; }
}
[/Quote]

familyX 2008-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; charset=gb2312" />
<title>无标题文档</title>

<script type="text/javascript">
function xy(){
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "text" || inputs[i].type == "password") {
inputs[i].className = "dd";
}
}
}
</script>
<style type="text/css">
<!--
.dd {
font-family: "宋体";
border-top-color: #003399;
border-right-color: #003399;
border-bottom-color: #003399;
border-left-color: #003399;
}
-->
</style>
</head>

<body onload="xy()">
<input type="text" name="t" id="t" />
</body>
</html>

myvicy 2008-06-04
  • 打赏
  • 举报
回复
更改className即可.注意N是大写的。
my_web 2008-06-04
  • 打赏
  • 举报
回复
inputs[i].className = '';

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧