例子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"> www.2cto.com
$(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文檔中目前就介紹了這些屬性設定方法
作者:yue7603835