JQuery學(xué)習(xí)筆記-操作DOM元素

1. attr()方法的作用是設(shè)置或者返回元素的屬性,其中attr(屬性名)格式是獲取元素屬性名的值,

成都創(chuàng)新互聯(lián)專注于大化網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供大化營(yíng)銷型網(wǎng)站建設(shè),大化網(wǎng)站制作、大化網(wǎng)頁設(shè)計(jì)、大化網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開發(fā)服務(wù),打造大化網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供大化網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

attr(屬性名,屬性值)格式則是設(shè)置元素屬性名的值。

2. 使用html()和text()方法操作元素的內(nèi)容,當(dāng)兩個(gè)方法的參數(shù)為空時(shí),表示獲取該元素的內(nèi)容,

而如果方法中包含參數(shù),則表示將參數(shù)值設(shè)置為元素內(nèi)容。

    html()方法可以獲取元素的HTML內(nèi)容,因此,原文中的格式代碼也被一起獲取,

    而text()方法只是獲取元素中的文本內(nèi)容,并不包含HTML格式代碼。

3. 通過addClass()和css()方法可以方便地操作元素中的樣式,前者括號(hào)中的參數(shù)為增加元素的樣式名稱,后者直接將樣式的屬性內(nèi)容寫在括號(hào)中。

$("#content").css("background-color", "Red");
 $("#content").css({"background-color":"red", "color":"white"});
. redBack{
	background-color : Red;
}
. whiteFont{
	color : white;
}
$("#content").addClass("redBack whiteFont");

4. 使用removeAttr(name)和removeClass(class)分別可以實(shí)現(xiàn)移除元素的屬性和樣式的功能,前者方法中參數(shù)表示移除屬性名,后者方法中參數(shù)則表示移除的樣式名。

   $("#content").removeClass("blue white");

   $("#content").removeAttr("href");

5. append(content)方法的功能是向指定的元素中追加內(nèi)容,被追加的content參數(shù),可以是字符、HTML元素標(biāo)記,還可以是一個(gè)返回字符串內(nèi)容的函數(shù)。

        <script type="text/javascript">
            function rethtml() {
                var $html = "<div id='test' title='hi'>我是調(diào)用函數(shù)創(chuàng)建的</div>"
                return $html;
            }
            $("body").append(rethtml());
        </script>

6. appendTo()方法也可以向指定的元素內(nèi)插入內(nèi)容,它的使用格式是:

$(content).appendTo(selector)

參數(shù)content表示需要插入的內(nèi)容,參數(shù)selector表示被選的元素,即把content內(nèi)容插入selector元素內(nèi),默認(rèn)是在尾部。

        <div>
            <span class="green">小烏龜</span>
        </div>
        <span class='red'>小青蛙</span>
        <script type="text/javascript">
            $(".red").appendTo("div");
        </script>

7. 使用before()和after()方法可以在元素的前后插入內(nèi)容,它們分別表示在整個(gè)元素的前面和后面插入指定的元素或內(nèi)容,調(diào)用格式分別為:

$(selector).before(content)和$(selector).after(content)

其中參數(shù)content表示插入的內(nèi)容,該內(nèi)容可以是元素或HTML字符串。

        <span class="green">我們交個(gè)朋友吧!</span>     
        <script type="text/javascript">
            var $html = "<span class='red'>兄弟。</span>"
            $(".green").after($html);
        </script>

8. 調(diào)用clone()方法可以生成一個(gè)被選元素的副本,即復(fù)制了一個(gè)被選元素,包含它的節(jié)點(diǎn)、文本和屬性,它的調(diào)用格式為:$(selector).clone(),其中參數(shù)selector可以是一個(gè)元素或HTML內(nèi)容。

9. replaceWith()和replaceAll()方法都可以用于替換元素或元素中的內(nèi)容,但它們調(diào)用時(shí),內(nèi)容和被替換元素所在的位置不同,分別為如下所示:

$(selector).replaceWith(content)和$(content).replaceAll(selector)

參數(shù)selector為被替換的元素,content為替換的內(nèi)容。

        <span class="green" title="hi">我是屌絲</span>
        
        <script type="text/javascript">
            var $html = "<span class='red' title='hi'>我是土豪</span>";
            $($html).replaceAll(".green");
        </script>

10. wrap()和wrapInner()方法都可以進(jìn)行元素的包裹,但前者用于包裹元素本身,后者則用于包裹元素中的內(nèi)容,它們的調(diào)用格式分別為:

$(selector).wrap(wrapper)和$(selector).wrapInner(wrapper)

參數(shù)selector為被包裹的元素,wrapper參數(shù)為包裹元素的格式。

	<span class="red" title='hi'>我的身體有點(diǎn)歪</span>      
        <script type="text/javascript">
            $(".red").wrapInner("<i></i>");
        </script>	
即<span class="red" title='hi'><i>我的身體有點(diǎn)歪</i></span>      

		<span class="red" title='hi'>我的身體有點(diǎn)歪</span>      
        <script type="text/javascript">
            $(".red").wrap("<i></i>");
        </script>	
即<i><span class="red" title='hi'>我的身體有點(diǎn)歪</span></i>

11. 使用each()方法可以遍歷指定的元素集合,在遍歷時(shí),通過回調(diào)函數(shù)返回遍歷元素的序列號(hào),它的調(diào)用格式為:$(selector).each(function(index))

參數(shù)function為遍歷時(shí)的回調(diào)函數(shù),index為遍歷元素的序列號(hào),它從0開始。

	<span class="green">香蕉</span>
        <span class="green">桃子</span>
        <span class="green">葡萄</span>
        <span class="green">荔枝</span>
        
        <script type="text/javascript">
            $("span").each(function (index) {
                if (index == 1) {
                    $(this).attr("class", "red");
                }
            });
        </script>

 

12. remove()方法刪除所選元素本身和子元素,該方法可以通過添加過濾參數(shù)指定需要?jiǎng)h除的某些元素,而empty()方法則只刪除所選元素的子元素。

        <span class="green">香蕉</span>
        <span class="green">桃子</span>
        <span class="red">葡萄</span>
        <span class="green">荔枝</span>
        
        <script type="text/javascript">
            $("span").empty();
			$("span").remove(".red");
        </script>

13. 使用attr方法,取消id號(hào)為test的復(fù)選框選中狀態(tài)代碼為:

$("#test").attr("checked", false);

14. 在三個(gè)元素中,刪除第二個(gè)元素的代碼為$("div:eq(1)").remove();

網(wǎng)站名稱:JQuery學(xué)習(xí)筆記-操作DOM元素
網(wǎng)頁網(wǎng)址:http://muchs.cn/article10/ihcogo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、域名注冊(cè)、微信公眾號(hào)響應(yīng)式網(wǎng)站、App設(shè)計(jì)企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)