jquery?操作DOM的基本用法分享
例子展示:
jquery代碼:
<script language="javascript">
$(document).ready(function(){
alert($("ul li:eq(1)").text()); //選取第二個(gè)li的值
alert($("p").attr("title")); //選取p的title屬性的值
//追加元素
$('ul').append("<li title='香蕉'>香蕉</li>").append("<li title='雪梨'>雪梨</li>");
//前面追加
$('ul').prepend("<li title='欠佳'>前加</li>");
//后面追加
$('ul').after("<li title='后加'>后加</li>");
//在p后面添加
$("<b> 你好</b>").insertAfter("p");
//在p前面添加
$("<b> 你好</b>").insertBefore("p");
//刪除節(jié)點(diǎn)
$("ul :eq(1)").remove();
// 清空值
$("ul :eq(2)").empty();
//復(fù)制節(jié)點(diǎn)
$("ul li").click(function(){
$(this).clone(true).appendTo("ul");//true可有可無(wú),有表示在復(fù)制的時(shí)候同時(shí)把綁定的事件也復(fù)制上
});
//替換節(jié)點(diǎn)
$("p[title=test]").replaceWith("<strong>你最喜歡的水果是?</strong>");
//$("<strong>你最喜歡的水果是?</strong>").replaceAll("P");
//包裹事件
$("strong").wrap("<b></b>")
//屬性操作
$("P").attr({"title":"test","name":"pName"}); //添加屬性
$("p").removeAttr("title"); //移除屬性
//樣式的操作
/*
添加樣式: $("p").addClass("hight");
已出樣式: $("p").removeClass("highr");
切換樣式: $("p").toggleClass("another");
是否有樣式: $("p").hasCLass("higth");
*/
alert($("p").html()); //獲取值 html()類(lèi)似于javascript中的innerHTML屬性
$("p").html("change"); //改變值
alert($("p").text()); //獲取值 text()類(lèi)似于javascript中的innerTEXT屬性
$("p").text("again change"); //改變值
$("#name").focus(function(){
if($("#name").val()=="請(qǐng)輸入用戶(hù)名"){
$(this).val("");
}
}).blur(function(){
if($("#name").val()==""){
$(this).val("請(qǐng)輸入用戶(hù)名");
}
});
$("#password").focus(function(){
$("#tip").hide();
}).blur(function(){
if($("#password").val()==""){
$("#tip").show(200);
}
});
$("#submit").click(function(){
if($("#name").val()=="請(qǐng)輸入用戶(hù)名"||$("#password").val()==""){
$("#name").css("background","yellow");
$("#password").css("background","yellow");
}
});
$("#single").val("選擇2");
$("#multiple").val(["選擇2號(hào)","選擇3號(hào)"]);
$(":checkbox").val(["check2","check3"]);
$(":radio").val(["radio1"]);
alert("careful!");
$("#single :eq(2)").attr("selected",true);
$("[value=radio2]:radio").attr("checked",true);
//遍歷節(jié)點(diǎn) children()方法
$("#btnShow").click(function(){
for(var i=0;i<$("body").children().length;i++){
$("#body").append($("body").children()[i].innerHTML);
}
});
//next()方法,取得緊挨p后面的同輩的所有元素
alert($("ul li").next().text());
//prev()方法,取得緊挨匹配前面的同輩的一個(gè)元素
alert($("li[title=菠蘿]").prev().text());
//siblings()方法,獲取匹配元素所有的同輩元素
for(var i=0;i<$("p").siblings().length;i++){
$("#subling").append($("p").siblings()[i].innerHTML);
}
//closest()方法,取得最近的匹配元素
$(document).bind("click",function(e){
$(e.target).closest("li").css("color","red");
});
//css的操作
$("p").css({"fontSize":"40px","background":"yellow"});
//offset()方法,獲取元素在當(dāng)前視窗的相對(duì)偏移量,返回對(duì)象為top和left兩個(gè)屬性
alert("top="+$("P").offset().top +";"+"left="+$("P").offset().left);
//position()方法,獲取元素相對(duì)于最近的position樣式設(shè)置為relative或者absolute
//的祖父節(jié)點(diǎn)的相對(duì)偏移,返回top和left兩個(gè)屬性
alert("top="+$("P").position().top +";"+"left="+$("P").position().left);
//scrollTop() and scrollLest()方法返回滾動(dòng)條距頂端和左端的距離
alert("scrolltop="+$("P").scrollTop() +";"+"scrollleft="+$("P").scrollLeft());
});
</script>html代碼:
<body> <p >test</p> <p id="p" title="test">你喜歡的蘋(píng)果是?</p> <ul> <li title="蘋(píng)果">蘋(píng)果</li> <li title="橘子">橘子</li> <li title="菠蘿">菠蘿</li> </ul> <input type="button" id="show" value="show" /> <br/><br/><br/> <form id="form1" action="#"> <div > <input type="text" id="name" value="請(qǐng)輸入用戶(hù)名"><br/> <input type="password" id="password" value=""> <br/> <div id="tip" style="display:none"><font color="red">請(qǐng)輸入密碼</font></div><br/> <input type="button" id="submit" value="提交"/> </div> </form> <br/> <form id="from2" action="#"> <select id="single"> <option value="選擇1號(hào)">選擇1號(hào)</option> <option value="選擇2號(hào)">選擇2號(hào)</option> <option value="選擇2號(hào)">選擇3號(hào)</option> </select> <select id="multiple" multiple="multiple" style="height:120px"> <option selected="selected">選擇1號(hào)</option> <option value="選擇2號(hào)">選擇2號(hào)</option> <option value="選擇3號(hào)">選擇3號(hào)</option> <option value="選擇4號(hào)">選擇4號(hào)</option> <option selected="selected">選擇5號(hào)</option> </select> <input type="checkbox" value="check1"/>多選1 <input type="checkbox" value="check2"/>多選2 <input type="checkbox" value="check3"/>多選3 <input type="checkbox" value="check4"/>多選4 <input type="radio" name="radio" value="radio1"/> 單選1 <input type="radio" name="radio" value="radio2"/> 單選2 <input type="radio" name="radio" value="radio3"/> 單選3 <br/> <input type="button" id="btnShow" value="顯示body"/> <br/><div id="body"></div> <div id="subling"></div> </form> </script> </body>
到此這篇關(guān)于jquery 操作DOM的基本用法分享的文章就介紹到這了,更多相關(guān)jquery 操作DOM內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jQuery實(shí)現(xiàn)仿美橙互聯(lián)兩級(jí)導(dǎo)航菜單的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)仿美橙互聯(lián)兩級(jí)導(dǎo)航菜單的方法,實(shí)例分析了jQuery操作css及setTimeout等實(shí)現(xiàn)導(dǎo)航菜單的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
jQuery代碼實(shí)現(xiàn)對(duì)話框右上角菜單帶關(guān)閉×
這篇文章主要介紹了jQuery代碼實(shí)現(xiàn)對(duì)話框右上角菜單帶關(guān)閉× 的相關(guān)資料,需要的朋友可以參考下2016-05-05
自己動(dòng)手制作jquery插件之自動(dòng)添加刪除行的實(shí)現(xiàn)
我們常常會(huì)遇到這樣的情況,一個(gè)系統(tǒng)中有大量的需要對(duì)一個(gè)行進(jìn)行復(fù)制添加,希望能夠進(jìn)行批量的操作,這個(gè)時(shí)候,我們就可以考慮把它做成一個(gè)jquery插件了.2011-10-10
用戶(hù)管理的設(shè)計(jì)_jquery的ajax實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果
下面小編就為大家?guī)?lái)一篇用戶(hù)管理的設(shè)計(jì)_jquery的ajax實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
jQuery實(shí)現(xiàn)的左右移動(dòng)焦點(diǎn)圖效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)的左右移動(dòng)焦點(diǎn)圖效果,涉及jQuery結(jié)合時(shí)間函數(shù)響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素樣式的相關(guān)技巧,需要的朋友可以參考下2016-01-01
jQuery移動(dòng)端日期(datedropper)和時(shí)間(timedropper)選擇器附源碼下載
今天我給大家介紹一款非常有趣的日期和時(shí)間選擇器,它分為日期選擇器datedropper以及時(shí)間選擇器timedropper,他們倆尤其適合在移動(dòng)端上應(yīng)用。感興趣的小伙伴一起學(xué)習(xí)吧2016-04-04

