亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

jQuery DOM操作replaceWith()和replaceAll()

之前學習了節(jié)點的內插入、外插入以及刪除方法,這節(jié)會學習替換方法replaceWith

.replaceWith( newContent ):用提供的內容替換集合中所有匹配的元素并且返回被刪除元素的集合

簡單來說:用$()選擇節(jié)點A,調用replaceWith方法,傳入一個新的內容B(HTML字符串,DOM元素,或者jQuery對象)用來替換選中的節(jié)點A

看個簡單的例子:一段HTML代碼

<div>
   <p>第一段</p>
   <p>第二段</p>
   <p>第三段</p>
</div>

替換第二段的節(jié)點與內容

$("p:eq(1)").replaceWith('<a style="color:red">替換第二段的內容</a>')

通過jQuery篩選出第二個p元素,調用replaceWith進行替換,結果如下

<div>
   <p>第一段</p>
   <a style="color:red">替換第二段的內容</a>'
   <p>第三段</p>
</div>


.replaceAll( target ) :用集合的匹配元素替換每個目標元素

.replaceAll()和.replaceWith()功能類似,但是目標和源相反,用上述的HTML結構,我們用replaceAll處理

$('<a style="color:red">替換第二段的內容</a>').replaceAll('p:eq(1)')

總結:

    replaceAll()和.replaceWith()功能類似,主要是目標和源的位置區(qū)別

    replaceWith()與.replaceAll() 方法會刪除與節(jié)點相關聯(lián)的所有數(shù)據(jù)和事件處理程序

    replaceWith()方法,和大部分其他jQuery方法一樣,返回jQuery對象,所以可以和其他方法鏈接使用

    replaceWith()方法返回的jQuery對象引用的是替換前的節(jié)點,而不是通過replaceWith/replaceAll方法替換后的節(jié)點

看下面的實例:

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
    <style>

    .right div {
        background: yellow;
    }
    </style>
</head>

<body>
    <h2>replaceWith()和replaceAll()</h2>
    <div class="left">
        <button class="bt1">點擊,通過replaceWith替換內容</button>
        <button class="bt2">點擊,通過rreplaceAll替換內容</button>
    </div>
    <div class="right">
        <div>
            <p>第一段</p>
            <p>第二段</p>
            <p>第三段</p>
        </div>
        <div>
            <p>第四段</p>
            <p>第五段</p>
            <p>第六段</p>
        </div>
    </div>
    <script type="text/javascript">
    //只克隆節(jié)點
    //不克隆事件
    $(".bt1").click(function(){
        //找到內容為第二段的p元素
        //通過replaceWith刪除并替換這個節(jié)點
        $(".right > div:first p:eq(1)").replaceWith('<a style="color:red">replaceWith替換第二段的內容</a>')
    })
    </script>
    <script type="text/javascript">
    //找到內容為第六段的p元素
    //通過replaceAll刪除并替換這個節(jié)點
    $(".bt2").click(function() {
        $('<a style="color:red">replaceAll替換第六段的內容</a>').replaceAll('.right > div:last p:last');
    })
    </script>
</body>

</html>


Weiter lernen
||
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title></title> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> <style> .right div { background: yellow; } </style> </head> <body> <h2>replaceWith()和replaceAll()</h2> <div class="left"> <button class="bt1">點擊,通過replaceWith替換內容</button> <button class="bt2">點擊,通過rreplaceAll替換內容</button> </div> <div class="right"> <div> <p>第一段</p> <p>第二段</p> <p>第三段</p> </div> <div> <p>第四段</p> <p>第五段</p> <p>第六段</p> </div> </div> <script type="text/javascript"> //只克隆節(jié)點 //不克隆事件 $(".bt1").click(function(){ //找到內容為第二段的p元素 //通過replaceWith刪除并替換這個節(jié)點 $(".right > div:first p:eq(1)").replaceWith('<a style="color:red">replaceWith替換第二段的內容</a>') }) </script> <script type="text/javascript"> //找到內容為第六段的p元素 //通過replaceAll刪除并替換這個節(jié)點 $(".bt2").click(function() { $('<a style="color:red">replaceAll替換第六段的內容</a>').replaceAll('.right > div:last p:last'); }) </script> </body> </html>
einreichenCode zurücksetzen