简体   繁体   中英

How to remove div elements within specific div id

I want to remove all div elements with class facebook , twitter , LinkedIn , googlePlus who has parent id show-share-count

How to do this?

 $("div").remove(".facebook"); $("div").remove(".twitter"); $("div").remove(".linkedIn"); $("div").remove(".googlePlus"); 
 <script src="https://code.jquery.com/jquery-1.10.2.js"></script> <div id="show-share-count"> <div class="facebook">Facebook</div> <div class="twitter">Twitter</div> <div class="linkedIn">LinkedIn</div> <div class="googlePlus">Google Plus</div> <div class="share-count">Total Count</div> </div> <br /><br /> <div class="facebook">Facebook</div> <div class="twitter">Twitter</div> <div class="linkedIn">LinkedIn</div> <div class="googlePlus">Google Plus</div> <div class="share-count">Total Count</div> 

you can do this just only one line in script section.

<script>
$("#show-share-count").find(".facebook, .twitter, .linkedIn, .googlePlus").remove();
</script>

Just use following script for this

$('#show-share-count .facebook, #show-share-count .twitter, #show-share-count .linkedIn, #show-share-count .googlePlus, #show-share-count .share-count').remove();

or you can use .children() ..

$("#show-share-count").children(".facebook , .twitter , .linkedIn, .googlePlus").remove();

Do like this . children() .They will find the children element of the show-share-count

 $("#show-share-count").children(".facebook , .twitter , .linkedIn, .googlePlus").remove() 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="show-share-count"> <div class="facebook">Facebook</div> <div class="twitter">Twitter</div> <div class="linkedIn">LinkedIn</div> <div class="googlePlus">Google Plus</div> <div class="share-count">Total Count</div> </div> <br /><br /> <div class="facebook">Facebook</div> <div class="twitter">Twitter</div> <div class="linkedIn">LinkedIn</div> <div class="googlePlus">Google Plus</div> <div class="share-count">Total Count</div> </div> 

This is how you can do it:

$("button").click(function() {
  $("#show-share-count").find(".facebook, .twitter, .linkedIn, .googlePlus, .share-count").remove();
});

Here is the JSFiddle demo

You can try to use .empty from Jquery if you want to empty the whole div

https://api.jquery.com/empty/

or

$("#show-share-count .facebook").remove() 

if you want to remove a specific div

您还可以在下面使用:

$('#show-share-count .facebook,.twitter,.linkedIn,.googlePlus,.share-count').remove();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM