jQuery (5)-Addition, deletion and modification of Dom

Addition, deletion and modification of Dom

1 Introduction

Internal insertion: Father-son relationship

  1. appendTo() a.appendTo(b) inserts a at the end of the child element of b and becomes the last child element
  2. prependTo() a.prependTo(b) insert a in front of all child elements of b and become the first child element

External insertion: Brotherhood

  1. insertAfter()   a.insertAfter(b)   得到 ba
  2. insertBefore()  a.insertBefore(b)   得到 ab

replace:

  1. replaceWith() a.replaceWith(b) replace a with b  Leave b; replace all a with a b
  2. replaceAll() a.replaceAll(b) Replace all b with a  Leave a; replace a b with one a

delete: No parameters

  1. remove() a.remove(); remove a tag
  2. empty() a.empty(); Empty the content in the a tag

Element parameters in (): take appendTo() as an example

  1. Can be ("div")
  2. You can also check it yourself, ($("div"))

2. AppendTo practice

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery-2021-01-31</title>
    <style type="text/css">
		select {
     
     
			width: 100px;
			height: 180px;
		}

		div {
     
     
			width: 130px;
			float: left;
			text-align: center;
		}
	</style>
     <script type="text/javascript" src="../script/jquery-1.7.2.js"></script>
     <script type="text/javascript">
--------------------功能在此完善--------------------
     </script>
</head>
<body>
    <div id="left">
		<select multiple="multiple" name="sel01">
			<option value="opt01">选项1</option>
			<option value="opt02">选项2</option>
			<option value="opt03">选项3</option>
			<option value="opt04">选项4</option>
			<option value="opt05">选项5</option>
			<option value="opt06">选项6</option>
			<option value="opt07">选项7</option>
			<option value="opt08">选项8</option>
		</select>

		<button>选中添加到右边</button>
		<button>全部添加到右边</button>
	</div>
	<div id="rigth">
		<select multiple="multiple" name="sel02">
		</select>
		<button>选中删除到左边</button>
		<button>全部删除到左边</button>
	</div>
</body>
</html>

The interface is as follows, complete the following functions:

<script type="text/javascript" src="../script/jquery-1.7.2.js"></script>
<script type="text/javascript">
        $(function () {
     
     
           // 第一个按钮 【选中添加到右边】
            $("button:eq(0)").click(function () {
     
     
                $("select:eq(0) > option:selected").appendTo($("select:eq(1)"));
            });
            
            // 第二个按钮 【全部添加到右边】
            $("button:eq(1)").click(function () {
     
     
                $("select:eq(0) > option").appendTo($("select:eq(1)"));
            });
            
            // 第三个按钮 【选中删除到左边】
			$("button:eq(2)").click(function () {
     
     
				$("select:eq(1) option:selected").appendTo($("select:eq(0)"));
			});         由于只有父子关系,optipn下面没有子元素,所以加不加>都行,加了更严谨

			// 第四个按钮 【全部删除到左边】
			$("button:eq(3)").click(function () {
     
     
				$("select:eq(1) option").appendTo($("select:eq(0)"));
			});
        });
</script>

3. Replacement test

<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="../script/jquery-1.7.2.js"></script>
    <script type="text/javascript">
        $(function () {
     
     
----------------填入两个测试语句:replaceWith()和replaceAll()----------------
        });
    </script>
</head>
<body>
    <br/><br/>
    <div>1234</div>
    <h5>我是陆亿行</h5>
    <div>5678</div>
</body>
</html>

Before filling:

  1. $("div").replaceWith( $("<h1>我才是陆亿行</h1>") );
    All div tags are replaced by () tags in parentheses

  2. $("<h1>我才是陆亿行</h1>").replaceAll( "div" );
    Replace one by one

Guess you like

Origin blog.csdn.net/HangHug_L/article/details/113480887