jQuery的DOM操作

属性操作:

attr()、removeAttr()
<hr>
    <h2>属性操作:attr() and removeAttr()</h2>
    <hr>
    <label for="">属性操作:</label><span id="attr_get">获取属性:</span><span id="attr_set">修改属性:</span><span id="attr_del">删除属性:</span>
    <div id="box" style="width: 100%;height: 500px;background:gray">
        <input style="width: 50%;" type="text" placeholder="属性测试">
    </div>
    <script>
        $(function(){
            // 属性操作 : 获取  修改  删除
            $('#attr_get').click(function(){
                $('#box input').val($('#attr_get').text()+$('#box').attr('style'));
            });
            $('#attr_set').click(function(){
                $('#box').attr('style', 'background:red;color:white;width: 100%;height: 500px;');
                $('#box input').val($('#attr_set').text()+$('#box').attr('style'));
            });
            $('#attr_del').click(function(){
                $('#box input').val($('#attr_del').text()+$('#box').removeAttr('style'));
            });
        });
    </script>
    ```
### 内容节点获取 :
###### html() 、text()、val()
	```
	<br>
    <br>
    <br>
    <br>
    <hr>
    <h2>节点内容获取:html() and text() and val()</h2>
    <hr>
    <label for="">节点获取:</label>
    <span id="node_get_html">node_get_html:</span>
    <span id="node_get_text">node_get_text:</span>
    <span id="node_get_val">node_get_val:</span>
    <div id="node_get"><sapn>获取值:html() and text() and val()</sapn></div>
    <input type="text" id="node_get_ipt" value=".val()的值获取">
    <script>
        $(function(){
            $('#node_get_html').click(function(){
                alert('html():'+$('#node_get').html());
            });
            $('#node_get_text').click(function(){
                alert('text():'+$('#node_get').text());
            });
            $('#node_get_val').click(function(){
                alert('val():'+$('#node_get_ipt').val());
            });
        })
        </script> 

节点内容设置:

html() and text() and val()
<br>
        <br>
        <br>
        <br>
        <hr>
        <h2>节点内容设置:html() and text() and val()</h2>
        <hr>
        <label for="">节点设置:</label>
        <span id="node_set_html">node_set_html:</span>
        <span id="node_set_text">node_set_text:</span>
        <span id="node_set_val">node_set_val:</span>
        <div >设置值:<sapn id="node_set">html() and text() and val()</sapn></div>
        <input type="text" id="node_set_ipt" value=".val()的值获取">
        <script>
            $(function(){
                $('#node_set_html').click(function(){
                    $('#node_set').html('html设置值');
                });
                $('#node_set_text').click(function(){
                    $('#node_set').text('text设置值');
                });
                $('#node_set_val').click(function(){
                    $('#node_set_ipt').val('val设置值');
                });
            })
        </script>

文档处理:

添加:append and appendTo and prepend and prependTo
 <br>
            <br>
            <br>
            <br>
            <hr>
            <h2>文档处理:append and appendTo and prepend and prependTo and remove and empty</h2>
            <h2>添加到选择元素里面</h2>
            <hr>
            <label for="">文档操作展示:</label>
            <span id="dom_add">后面追加[append]</span>
            <span id="dom_pre_add">前面追加[prepend]</span>
            <span id="dom_to_add">后面追加到[appendTo]</span>
            <span id="dom_pre_to_add">前面追加到[prependTo]</span>
            <span id="dom_remove">全部删除[remove]</span>
            <span id="dom_empty">删除所选元素的子元素[empty]</span>
            <ul id="fruits">
                <li>苹果</li>
                <li>哈密瓜</li>
                <li id="banana">香蕉</li>
                <li>菠萝</li>
            </ul>
            <script>
                $(function(){
                    $('#dom_add').click(function(){
                        $('#fruits').append('<li>橘子</li>');
                    });
                    $('#dom_pre_add').click(function(){
                        $('#fruits').prepend('<li>橘子</li>');
                    });
                    $('#dom_to_add').click(function(){
                        $('<li>橘子</li>').appendTo('#fruits');
                    });
                    $('#dom_pre_to_add').click(function(){
                        $('<li>橘子</li>').prependTo('#fruits');
                    });
                    $('#dom_remove').click(function(){
                        $('#fruits').remove();
                    });
                    $('#dom_empty').click(function(){
                        $('#fruits').empty('#banana');
                    });
                    });
            </script>

添加:before and after and insert before and insert after
  <br>
            <br>
            <br>
            <br>
            <hr>
            <h2>文档处理:before and after and insert before and insert after and remove and empty</h2>
            <h2>添加到选择元素外面</h2>
            <hr>
            <label for="">文档操作展示:</label>
            <span id="dom_after">后面追加[after]</span>
            <span id="dom_before">前面追加[before]</span>
            <span id="dom_insert_after">后面追加到[insert after]</span>
            <span id="dom_insert_before">前面追加到[insert before]</span>
            <span id="dom_remove_a">全部删除[remove]</span>
            <span id="dom_empty_a">删除所选元素的子元素[empty]</span>
            <ul id="fruits_a">
                <li>苹果</li>
                <li>哈密瓜</li>
                <li class="banana">香蕉</li>
                <li>菠萝</li>
            </ul>
            <script>
                $(function(){
                    $('#dom_after').click(function(){
                        $('#fruits_a').after('<li>橘子</li>');
                    });
                    $('#dom_before').click(function(){
                        $('#fruits_a').before('<li>橘子</li>');
                    });
                    $('#dom_insert_after').click(function(){
                        $('<li>橘子</li>').insertAfter('#fruits_A');
                    });
                    $('#dom_insert_before').click(function(){
                        $('<li>橘子</li>').insertBefore('#fruits_a');
                    });
                    $('#dom_remove_a').click(function(){
                        $('#fruits_a').remove();
                    });
                    $('#dom_empty_a').click(function(){
                        $('#fruits_a').empty('.banana');
                    });
                    });
            </script>
包裹:wrap and unwrap
<br>
               <br>
               <br>
               <br>
               <hr>
               <h2>文档处理:wrap and unwrap</h2>
               <h2>包裹</h2>
               <hr>
               <label for="">文档操作展示:</label>
               <span id="dom_wrap">包裹[wrap] 操作</span>
               <span id="dom_unwrap">去除包裹[unwrap] 操作</span>
               <div id="baoguo">
                    <p>hello</p>
                    <p>gilrs</p>
                    <p>!</p>
                    <br>
                    <br>
                    <br>
               </div>
               <script>
                   $(function(){
                       $('#dom_wrap').click(function(){
                           // 给每个p标签外面包裹一个div
                           $('#baoguo p').wrap('<div style="color:red;"></div>');
                       });
                       $('#dom_unwrap').click(function(){
                           $('#baoguo p').unwrap(); //删除之后会继续向上删除
                       });
                   });
               </script>

删除清空:clone and remove and empty

<br>
<br>
<br>
<br>
<hr>
<h2>文档处理:clone and remove and empty</h2>
<h2>添加到选择元素里面</h2>
<hr>
<label for="">文档操作展示:</label>
<span id="dom_remove_b">删除[remove]</span>
<span id="dom_empty_b">清空[empty]</span>
<span id="dom_clone_b">克隆[clone]</span>
<ul id="fruits_b">
    <li id="node_remove">remove</li>
    <li id="node_empty">empty</li>
    <li id="node_clone">clone</li>
</ul>
<script>
    $(function(){
        $('#dom_remove_b').click(function(){
            $('#node_remove').remove();
        });
        $('#dom_empty_b').click(function(){
            $('#node_empty').empty();
        });
        $('#dom_clone_b').click(function(){
            alert($('#node_clone').clone().text());
        });
        });
</script>

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <style>
        span{
            border: 1px solid #666;
            margin: 5px 10px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <hr>
    <h2>属性操作:attr() and removeAttr()</h2>
    <hr>
    <label for="">属性操作:</label><span id="attr_get">获取属性:</span><span id="attr_set">修改属性:</span><span id="attr_del">删除属性:</span>
    <div id="box" style="width: 100%;height: 500px;background:gray">
        <input style="width: 50%;" type="text" placeholder="属性测试">
    </div>
    <script>
        $(function(){
            // 属性操作 : 获取  修改  删除
            $('#attr_get').click(function(){
                $('#box input').val($('#attr_get').text()+$('#box').attr('style'));
            });
            $('#attr_set').click(function(){
                $('#box').attr('style', 'background:red;color:white;width: 100%;height: 500px;');
                $('#box input').val($('#attr_set').text()+$('#box').attr('style'));
            });
            $('#attr_del').click(function(){
                $('#box input').val($('#attr_del').text()+$('#box').removeAttr('style'));
            });
        });
    </script>
    <br>
    <br>
    <br>
    <br>
    <hr>
    <h2>节点内容获取:html() and text() and val()</h2>
    <hr>
    <label for="">节点获取:</label>
    <span id="node_get_html">node_get_html:</span>
    <span id="node_get_text">node_get_text:</span>
    <span id="node_get_val">node_get_val:</span>
    <div id="node_get"><sapn>获取值:html() and text() and val()</sapn></div>
    <input type="text" id="node_get_ipt" value=".val()的值获取">
    <script>
        $(function(){
            $('#node_get_html').click(function(){
                alert('html():'+$('#node_get').html());
            });
            $('#node_get_text').click(function(){
                alert('text():'+$('#node_get').text());
            });
            $('#node_get_val').click(function(){
                alert('val():'+$('#node_get_ipt').val());
            });
        })
    </script>
        <br>
        <br>
        <br>
        <br>
        <hr>
        <h2>节点内容设置:html() and text() and val()</h2>
        <hr>
        <label for="">节点设置:</label>
        <span id="node_set_html">node_set_html:</span>
        <span id="node_set_text">node_set_text:</span>
        <span id="node_set_val">node_set_val:</span>
        <div >设置值:<sapn id="node_set">html() and text() and val()</sapn></div>
        <input type="text" id="node_set_ipt" value=".val()的值获取">
        <script>
            $(function(){
                $('#node_set_html').click(function(){
                    $('#node_set').html('html设置值');
                });
                $('#node_set_text').click(function(){
                    $('#node_set').text('text设置值');
                });
                $('#node_set_val').click(function(){
                    $('#node_set_ipt').val('val设置值');
                });
            })
        </script>
            <br>
            <br>
            <br>
            <br>
            <hr>
            <h2>文档处理:append and appendTo and prepend and prependTo and remove and empty</h2>
            <h2>添加到选择元素里面</h2>
            <hr>
            <label for="">文档操作展示:</label>
            <span id="dom_add">后面追加[append]</span>
            <span id="dom_pre_add">前面追加[prepend]</span>
            <span id="dom_to_add">后面追加到[appendTo]</span>
            <span id="dom_pre_to_add">前面追加到[prependTo]</span>
            <span id="dom_remove">全部删除[remove]</span>
            <span id="dom_empty">删除所选元素的子元素[empty]</span>
            <ul id="fruits">
                <li>苹果</li>
                <li>哈密瓜</li>
                <li id="banana">香蕉</li>
                <li>菠萝</li>
            </ul>
            <script>
                $(function(){
                    $('#dom_add').click(function(){
                        $('#fruits').append('<li>橘子</li>');
                    });
                    $('#dom_pre_add').click(function(){
                        $('#fruits').prepend('<li>橘子</li>');
                    });
                    $('#dom_to_add').click(function(){
                        $('<li>橘子</li>').appendTo('#fruits');
                    });
                    $('#dom_pre_to_add').click(function(){
                        $('<li>橘子</li>').prependTo('#fruits');
                    });
                    $('#dom_remove').click(function(){
                        $('#fruits').remove();
                    });
                    $('#dom_empty').click(function(){
                        $('#fruits').empty('#banana');
                    });
                    });
            </script>

            <br>
            <br>
            <br>
            <br>
            <hr>
            <h2>文档处理:before and after and insert before and insert after and remove and empty</h2>
            <h2>添加到选择元素外面</h2>
            <hr>
            <label for="">文档操作展示:</label>
            <span id="dom_after">后面追加[after]</span>
            <span id="dom_before">前面追加[before]</span>
            <span id="dom_insert_after">后面追加到[insert after]</span>
            <span id="dom_insert_before">前面追加到[insert before]</span>
            <span id="dom_remove_a">全部删除[remove]</span>
            <span id="dom_empty_a">删除所选元素的子元素[empty]</span>
            <ul id="fruits_a">
                <li>苹果</li>
                <li>哈密瓜</li>
                <li class="banana">香蕉</li>
                <li>菠萝</li>
            </ul>
            <script>
                $(function(){
                    $('#dom_after').click(function(){
                        $('#fruits_a').after('<li>橘子</li>');
                    });
                    $('#dom_before').click(function(){
                        $('#fruits_a').before('<li>橘子</li>');
                    });
                    $('#dom_insert_after').click(function(){
                        $('<li>橘子</li>').insertAfter('#fruits_A');
                    });
                    $('#dom_insert_before').click(function(){
                        $('<li>橘子</li>').insertBefore('#fruits_a');
                    });
                    $('#dom_remove_a').click(function(){
                        $('#fruits_a').remove();
                    });
                    $('#dom_empty_a').click(function(){
                        $('#fruits_a').empty('.banana');
                    });
                    });
            </script>
               <br>
               <br>
               <br>
               <br>
               <hr>
               <h2>文档处理:wrap and unwrap</h2>
               <h2>包裹</h2>
               <hr>
               <label for="">文档操作展示:</label>
               <span id="dom_wrap">包裹[wrap] 操作</span>
               <span id="dom_unwrap">去除包裹[unwrap] 操作</span>
               <div id="baoguo">
                    <p>hello</p>
                    <p>gilrs</p>
                    <p>!</p>
                    <br>
                    <br>
                    <br>
               </div>
               <script>
                   $(function(){
                       $('#dom_wrap').click(function(){
                           // 给每个p标签外面包裹一个div
                           $('#baoguo p').wrap('<div style="color:red;"></div>');
                       });
                       $('#dom_unwrap').click(function(){
                           $('#baoguo p').unwrap(); //删除之后会继续向上删除
                       });
                   });
               </script>

<br>
<br>
<br>
<br>
<hr>
<h2>文档处理:clone and remove and empty</h2>
<h2>添加到选择元素里面</h2>
<hr>
<label for="">文档操作展示:</label>
<span id="dom_remove_b">删除[remove]</span>
<span id="dom_empty_b">清空[empty]</span>
<span id="dom_clone_b">克隆[clone]</span>
<ul id="fruits_b">
    <li id="node_remove">remove</li>
    <li id="node_empty">empty</li>
    <li id="node_clone">clone</li>
</ul>
<script>
    $(function(){
        $('#dom_remove_b').click(function(){
            $('#node_remove').remove();
        });
        $('#dom_empty_b').click(function(){
            $('#node_empty').empty();
        });
        $('#dom_clone_b').click(function(){
            alert($('#node_clone').clone().text());
        });
        });
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值