本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。
在jquery中,可以通过修改属性值,或者移除属性来改变指定元素的属性。下面我们来具体了解一下。
1、使用attr()修改属性值
attr() 方法可以设置被选元素的属性值。
可将指定属性设置为新值
当没有指定属性时,可添加该属性
示例:修改input type属性,将文本框改为时间框
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$("input").attr("type","date");
});
});
</script>
</head>
<body class="ancestors">
<input type="text" value="John">
<br><br>
<button>修改input属性</button>
<p>修改input type属性,将文本框改为时间框</p>
</body>
</html>
登录后复制
2、使用removeAttr()删除属性
removeAttr() 方法从被选元素中移除属性。
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$("input").removeAttr("value");
});
});
</script>
</head>
<body class="ancestors">
<input type="text" value="John">
<br><br>
<button>修改input属性</button>
<p>删除input value属性,清空输入框</p>
</body>
</html>
登录后复制
【推荐学习:jQuery视频教程、web前端视频】
以上就是jquery如何改变input属性的详细内容,更多请关注靠谱客其它相关文章!
最后
以上就是爱撒娇铃铛最近收集整理的关于jquery如何改变input属性的全部内容,更多相关jquery如何改变input属性内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复