jQuery如何删除已有的HTML元素内容
这篇文章主要介绍了jQuery如何删除已有的HTML元素内容,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
jQuery - 删除元素
通过 jQuery,可以很容易地删除已有的 HTML 元素。
如需删除元素和内容,一般可使用以下两个 jQuery 方法:
remove() - 删除被选元素(及其子元素)
empty() - 从被选元素中删除子元素
jQuery remove() 方法
jQuery remove() 方法删除被选元素及其子元素。
<!DOCTYPEhtml><html><head><scriptsrc="../jquery/jquery-1.11.1.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("#div1").remove();});});</script></head><body><divid="div1"style="height:100px;width:300px;background-color:yellow;">Thisissometextinthediv.<p>Thisisaparagraphinthediv.</p><p>Thisisanotherparagraphinthediv.</p></div><br/><button>删除div元素</button></body></html>
jQuery empty() 方法
jQuery empty() 方法删除被选元素的子元素。
<!DOCTYPEhtml><html><head><scriptsrc="../jquery/jquery-1.11.1.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("#div1").empty();});});</script></head><body><divid="div1"style="height:100px;width:300px;background-color:yellow;">Thisissometextinthediv.<p>Thisisaparagraphinthediv.</p><p>Thisisanotherparagraphinthediv.</p></div><br/><button>清空div元素</button></body></html>
过滤被删除的元素
jQuery remove() 方法也可接受一个参数,允许您对被删元素进行过滤。
该参数可以是任何 jQuery 选择器的语法,下例中删除 class="italic" 的所有 <p> 元素:
<!DOCTYPEhtml><html><head><scriptsrc="../jquery/jquery-1.11.1.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("p").remove(".italic");});});</script></head><body><p>Thisisaparagraphinthediv.</p><pclass="italic"><i>Thisisanotherparagraphinthediv.</i></p><pclass="italic"><i>Thisisanotherparagraphinthediv.</i></p><button>删除class="italic"的所有p元素</button></body></html>
感谢你能够认真阅读完这篇文章,希望小编分享的“jQuery如何删除已有的HTML元素内容”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。