jQuery 노드 추가

노드 추가


아버지-자식 관계 추가

  • append(content): 일치하는 각 요소에 콘텐츠 추가

  • 앞에 추가(내용): 일치하는 각 요소에 콘텐츠 추가

  • appendTo(content): 일치하는 모든 요소를 ​​지정된 다른 요소 컬렉션에 추가

  • prependTo(content): 일치하는 모든 요소를 ​​지정된 다른 요소 집합 앞에 추가

예제는 다음과 같습니다.

<!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(){
            //主动追加
            //新节点追加
            //append() 后置 
            $("#yi").append("<li>成都市</li>");
            //prepend() 前置 
            $("#yi").prepend("<li>杭州市</li>");

            //已有节点追加(节点发生物理位置移动)
            $("#yi").append($("#gai"));
        }
        function f2(){
            //被动追加
            //appendTo  后置追加
            //prependTo  前置追加
            //新节点追加
            $("<li>武汉市</li>").appendTo('#yi');
            $("<li>天津市</li>").prependTo('#yi');
            //$("html标签")---创建节点--->createElement等方法
            //已有节点追加
            $('#er li:eq(1)').appendTo('#yi');
        }
        </script>
        <style type="text/css">
        div {width:300px; height:200px; background-color:pink;}
        </style>
    </head>
    <body>
        <ul id="yi">
            <li>北京</li>
            <li>上海</li>
            <li>广州</li>
        </ul>
        <ul id="er">
            <li>福州市</li>
            <li>合肥市</li>
            <li id="gai">郑州市</li>
        </ul>
        <input type="button" value="追加1" onclick="f1()" />
        <input type="button" value="追加2" onclick="f2()" />
    </body>
</html>


형제관계 추가

  • after(content): 일치하는 각 요소 뒤에 콘텐츠 삽입

  • 이전(내용 ): 일치하는 각 요소 앞에 콘텐츠를 삽입합니다.

  • insertAfter(content): 지정된 요소 집합 뒤에 일치하는 모든 요소를 ​​다른 요소에 삽입합니다.

  • insertBefore(content): 다른 지정된 요소 집합 앞에 일치하는 모든 요소를 ​​삽입합니다.

예제는 다음과 같습니다.

<!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(){
            //主动追加
            //after()  后置追加
            //before()  前置追加
            //新节点
            $('#yi li:last').after("<li>深圳</li>");
            $('#yi li:first').after("<li>成都市</li>");
            $('#fei').before("<li>杭州市</li>");
            //已有节点追加
            $('#fei').after($('#gai'));
        }
        </script>
        <style type="text/css">
        div {width:300px; height:200px; background-color:pink;}
        </style>
    </head>
    <body>
        <ul id="yi">
            <li>北京</li>
            <li id="fei">上海</li>
            <li>广州</li>
        </ul>
        <ul id="er">
            <li>福州市</li>
            <li>合肥市</li>
            <li id="gai">郑州市</li>
        </ul>
        <input type="button" value="追加1" onclick="f1()" />
    </body>
</html>


지속적인 학습
||
<!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(){ //主动追加 //新节点追加 //append() 后置 $("#yi").append("<li>成都市</li>"); //prepend() 前置 $("#yi").prepend("<li>杭州市</li>"); //已有节点追加(节点发生物理位置移动) $("#yi").append($("#gai")); } function f2(){ //被动追加 //appendTo 后置追加 //prependTo 前置追加 //新节点追加 $("<li>武汉市</li>").appendTo('#yi'); $("<li>天津市</li>").prependTo('#yi'); //$("html标签")---创建节点--->createElement等方法 //已有节点追加 $('#er li:eq(1)').appendTo('#yi'); } </script> <style type="text/css"> div {width:300px; height:200px; background-color:pink;} </style> </head> <body> <ul id="yi"> <li>北京</li> <li>上海</li> <li>广州</li> </ul> <ul id="er"> <li>福州市</li> <li>合肥市</li> <li id="gai">郑州市</li> </ul> <input type="button" value="追加1" onclick="f1()" /> <input type="button" value="追加2" onclick="f2()" /> </body> </html>
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!