jQuery节点替换

节点替换

  • replaceWith(content):将所以匹配的元素替换成指定HTML或DOM元素

  •             $("select").replaceWith(content);

  •             $('select')被content替换

  • replaceAll(selector):用匹配的元素替换掉所有selector匹配到的元素

  •             $('content').replaceAll(select);

  •             用content主动替换select元素

<!DOCTYPE html>
<html>
    <head>
        <title>php.cn</title>
        <meta charset="utf-8" />
        <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
        <script>
        function  f1(){
            //主动替换 replaceAll()
            $("#gai").replaceAll("#guan");
            //被动替换  replaceWith()
            $("#big li:first").replaceWith($('#small li:first'));
        }
        </script>
        <style type="text/css">
        div {width:300px; height:200px; background-color:pink;}
        </style>
    </head>
    <body>
        <h2>节点替换</h2>
        <ul id="big">
            <li>A</li>
            <li>B</li>
            <li id="guan">C</li>
        </ul>
        <ul id="small">
            <li>a</li>
            <li>b</li>
            <li id="gai">c</li>
        </ul>
        <input type="button" value="替换" onclick="f1()" />
    </body>
</html>
Weiter lernen
||
<!DOCTYPE html> <html> <head> <title>php.cn</title> <meta charset="utf-8" /> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> function f1(){ //主动替换 replaceAll() $("#gai").replaceAll("#guan"); //被动替换 replaceWith() $("#big li:first").replaceWith($('#small li:first')); } </script> <style type="text/css"> div {width:300px; height:200px; background-color:pink;} </style> </head> <body> <h2>节点替换</h2> <ul id="big"> <li>A</li> <li>B</li> <li id="guan">C</li> </ul> <ul id="small"> <li>a</li> <li>b</li> <li id="gai">c</li> </ul> <input type="button" value="替换" onclick="f1()" /> </body> </html>
einreichenCode zurücksetzen