jQuery文档分析4-属性的动态设置

例子1: 动态设置img的src属性 其他dom元素雷同 用于动态属性的设置
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(
function(){
$("#load").click(function(){
$("img").attr("src","admin.png") ; //设置img的src属性
$("img").attr("src") ;/ /返回所有img的src属性....
}
) ;
$("#unload").click(function(){
$("img").removeAttr("src") ; //删除img的src属性//
}) ;
}
) ;
</script>
<title>Insert title here</title>
</head>
<body>
<img src="">
<button id="load">显示</button>
<button id="unload">删除</button>
</body>
</html>
2、html text val的用法区别 ......
$('p').html();   是获取所有p标签下的html代码包括标签....
$('p').html("<a>xxx<a>"); 设置p下的html内容,和dom的innerHTML属性相对应  
$('p').text( );         同上设置或者获取..p标签下的所有文本...或者设置 ....这列的<a>dxx</a>都会解析成文本 
$("p").text("Hello world!");      
$("input").val();     获取或者设置 表单元素的值..具体可以结合筛选..选择器 选择我们的元素然后再获取或者设置值
$("input").val("hello world!");
3、通过动态设置dom元素的class 设置css属性
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
 $(document).ready(
 function(){
 $("#load").click(function(){ 
$("p").addClass("styleme") ;   //增加class属性 styleme
 }
 ) ;
$("#unload").click(function(){
 $("p").removeClass("styleme") ;  //卸载css属性
 
 }) ;
 } 
 ) ;
</script>
<style type="text/css">
.styleme{
 font-size: 20px ;
 color: blue; 
}
</style>
<title>Insert title here</title>
</head>
<body>
<p>23232d的</p>
<button id="load">显示</button>
<button id="unload">卸载</button>

</body>
</html>
$("p").toggleClass("selected");  //表示如果累不存在就添加一个类
4、设置/获取在匹配的元素集中的第一个元素的属性值。
$("input[type='checkbox']").prop("checked"); 获取input元素中type=checkbox 元素集合的第一个元素的checked属性值
$("input[type='checkbox']").prop("disabled", false);  禁用checkbox$("input[type='checkbox']").prop("checked", true);    设置checkbox选中、///
jQuery文档中目前就介绍了这些属性设置方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值