9--DOM operation in Jquery (insert node)

  1. append(content): Append content to the inner end of each matched element.
    Examples:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>插入节点</title>
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <ul id="fruit">
        <li title="pg">苹果</li>
        <li title="jz">橘子</li>
        <li title="xj">香蕉</li>
    </ul>
    <script>
        $(function(){
     
     
            /*创建节点*/
            var $bl=$("<li title='bl'>菠萝</li>");
            /*获取父节点*/
            var $ul=$("#fruit");
            /*在ul元素内部的结束位置添加新建的li元素*/
            $ul.append($bl);
        })
    </script>
</body>
</html>
  1. appendTo(content): Append each matched element node to the inner end of the specified element.
    Example 2:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>插入节点</title>
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <ul id="fruit">
        <li title="pg">苹果</li>
        <li title="jz">橘子</li>
        <li title="xj">香蕉</li>
    </ul>
    <script>
        $(function(){
     
     
            /*创建节点*/
            var $bl=$("<li title='bl'>菠萝</li>");
            /*获取父节点*/
            var $ul=$("#fruit");
            /*在ul元素内部的结束位置添加新建的li元素*/
            $bl.appendTo($ul);
        })
    </script>
</body>
</html>
  1. prepend(content): Append content to the beginning of each matched element.
    Example 3:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>插入节点</title>
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <ul id="fruit">
        <li title="pg">苹果</li>
        <li title="jz">橘子</li>
        <li title="xj">香蕉</li>
    </ul>
    <script>
        $(function(){
     
     
            /*创建节点*/
            var $bl=$("<li title='bl'>菠萝</li>");
            /*获取父节点*/
            var $ul=$("#fruit");
            /*在ul元素内部的开始位置添加新建的li元素*/
            $ul.prepend($bl);
        })
    </script>
</body>
</html>
  1. prependTo(content): Append each matched element node to the internal beginning of the specified element.
    Example 4:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>插入节点</title>
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <ul id="fruit">
        <li title="pg">苹果</li>
        <li title="jz">橘子</li>
        <li title="xj">香蕉</li>
    </ul>
    <script>
        $(function(){
     
     
            /*创建节点*/
            var $bl=$("<li title='bl'>菠萝</li>");
            /*获取父节点*/
            var $ul=$("#fruit");
            /*在ul元素内部的开始位置添加新建的li元素*/
            $bl.prependTo($ul);
        })
    </script>
</body>
</html>
  1. after(content): insert content after a certain element
    Example 5:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>插入节点</title>
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <ul id="fruit">
        <li title="pg">苹果</li>
        <li title="jz">橘子</li>
        <li title="xj">香蕉</li>
    </ul>
    <script>
        $(function(){
     
     
            /*创建节点*/
            var $bl=$("<li title='bl'>菠萝</li>");
            /*获取父节点*/
            var $ul=$("#fruit");
            /*在ul元素外部的结束位置添加新建的li元素*/
            $ul.after($bl);
        })
    </script>
</body>
</html>
  1. before(content): Insert content before a matched element.
    Example 6:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>插入节点</title>
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <ul id="fruit">
        <li title="pg">苹果</li>
        <li title="jz">橘子</li>
        <li title="xj">香蕉</li>
    </ul>
    <script>
        $(function(){
     
     
            /*创建节点*/
            var $bl=$("<li title='bl'>菠萝</li>");
            /*获取父节点*/
            var $ul=$("#fruit");
            /*在ul元素外部的结束位置添加新建的li元素*/
            $bl.insertAfter($ul);
        })
    </script>
</body>
</html>
  1. insertAfter(content): Insert all matched elements into the back of another set of specified elements.
    Example 7:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>插入节点</title>
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <ul id="fruit">
        <li title="pg">苹果</li>
        <li title="jz">橘子</li>
        <li title="xj">香蕉</li>
    </ul>
    <script>
        $(function(){
     
     
            /*创建节点*/
            var $bl=$("<li title='bl'>菠萝</li>");
            /*获取父节点*/
            var $ul=$("#fruit");
            /*在ul元素外部的结束位置添加新建的li元素*/
            $bl.insertBefore($ul);
        })
    </script>
</body>
</html>
  1. inertBefore(content): Insert all matched elements in front of another set of specified elements.
    Example 8:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>插入节点</title>
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <ul id="fruit">
        <li title="pg">苹果</li>
        <li title="jz">橘子</li>
        <li title="xj">香蕉</li>
    </ul>
    <script>
        $(function(){
     
     
            /*创建节点*/
            var $bl=$("<li title='bl'>菠萝</li>");
            /*获取父节点*/
            var $ul=$("#fruit");
            /*在ul元素外部的结束位置添加新建的li元素*/
            $ul.before($bl);
        })
    </script>
</body>
</html>

Guess you like

Origin blog.csdn.net/qwy715229258163/article/details/113868142