最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to delete a specific element inside a div using javascript? - Stack Overflow

programmeradmin1浏览0评论

I have an html code,

<div id="listing">
Text Message 
<div class="ch">ch 1</div>
<button class="btn" />
<div class="ch">ch 2</div>
<button class="btn" />
<div class="ch">ch 3</div>
<button class="btn" />
<div class="ch">ch 4</div>
<button class="btn" />
</div>

If a user click the button with class "btn", it will delete the "ch" div and the corresponding button. for example,

<div id="listing">
Text Message 
<div class="ch">ch 1</div>
<button class="btn" />
<div class="ch">ch 2</div>
<button class="btn" />
<div class="ch">ch 3</div>
<button class="btn" />
</div>

I tried to add ID of div. but It is difficult since the div with class "ch", is dynamically created.

So I want to delete a div that only have className.

I have an html code,

<div id="listing">
Text Message 
<div class="ch">ch 1</div>
<button class="btn" />
<div class="ch">ch 2</div>
<button class="btn" />
<div class="ch">ch 3</div>
<button class="btn" />
<div class="ch">ch 4</div>
<button class="btn" />
</div>

If a user click the button with class "btn", it will delete the "ch" div and the corresponding button. for example,

<div id="listing">
Text Message 
<div class="ch">ch 1</div>
<button class="btn" />
<div class="ch">ch 2</div>
<button class="btn" />
<div class="ch">ch 3</div>
<button class="btn" />
</div>

I tried to add ID of div. but It is difficult since the div with class "ch", is dynamically created.

So I want to delete a div that only have className.

Share Improve this question edited Apr 6, 2015 at 11:29 Pinas De Radio asked Apr 6, 2015 at 10:13 Pinas De RadioPinas De Radio 1232 silver badges7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

If a user click the div with class "ch", it will delete. for example,

I'm sorry, I pletely missed this when first answering your question.

Two ways you can do that:

  1. If you hook up a click handler, within the call to the handler, this will refer to the element that was clicked. So:

    var list = document.querySelectorAll("#listing .ch");
    var n;
    for (n = 0; n < list.length; ++n) {
        list[n].addEventListener("click", function() {
            this.parentNode.removeChild(this);
        }, false);
    }
    
  2. But since you say these are dynamically generated, I'd use event delegation, the fact that clicks bubble up to ancestor elements, and just hook click on #listing:

    document.getElementById("listing").addEventListener("click", function(e) {
        // Find the .ch, starting with the element the click originated in
        var ch = e.target;
        while (ch && !ch.className.match(/\bch\b/)) {
            ch = ch.parentNode;
        }
        if (ch) {
            ch.parentNode.removeChild(ch);
        }
    }, false);
    

Here's a live example of the delegation version:

document.getElementById("listing").addEventListener("click", function(e) {
  // Find the .ch, starting with the element the click originated in.
  // You need this loop if there's any possibility of any elements
  // *within* the .ch elements, such as `em` or `strong` or `span`.
  var ch = e.target;
  while (ch && !ch.className.match(/\bch\b/)) {
    ch = ch.parentNode;
  }
  if (ch) {
    ch.parentNode.removeChild(ch);
  }
}, false);
<div id="listing">
  Text Message
  <div class="ch">ch 1</div>
  <div class="ch"><em>ch 2</em></div>
  <div class="ch"><strong>ch 3</strong></div>
  <div class="ch">ch 4</div>
</div>


Original answer that missed that relevant point!

On any modern browser, you can do this:

document.querySelector("#listing div:nth-child(3)").remove();

Live example:

setTimeout(function() {
  document.querySelector("#listing div:nth-child(3)").remove();
}, 500);
<div id="listing">
Text Message 
<div class="ch">ch 1</div>
<div class="ch">ch 2</div>
<div class="ch">ch 3</div>
<div class="ch">ch 4</div>
</div>

On slightly older browsers you may need:

var element = document.querySelector("#listing div:nth-child(3)");
element.parentNode.removeChild(element);

Live example:

setTimeout(function() {
  var element = document.querySelector("#listing div:nth-child(3)");
  element.parentNode.removeChild(element);
}, 500);
<div id="listing">
Text Message 
<div class="ch">ch 1</div>
<div class="ch">ch 2</div>
<div class="ch">ch 3</div>
<div class="ch">ch 4</div>
</div>

Those both use :nth-child, which matches the nth child element inside its parent. (Note that it doesn't match the nth matching child, the div is just there for clarity.)

They also use querySelector, which is supported on all modern browsers, and also IE8.

    <div id="listing">
       Text Message 
       <div class="ch">ch 1</div>
       <div class="ch">ch 2</div>
       <div class="ch">ch 3</div>
      <div class="ch">ch 4</div>
    </div> 
    Simple use each function to get all divs
        $('.ch').click(function(){
          if($(this).text() == 'ch 1'){
            $(this).remove();
          }
        });
output:
<div id="listing">
       Text Message 
       <div class="ch">ch 2</div>
       <div class="ch">ch 3</div>
       <div class="ch">ch 4</div>
    </div> 
<div id="listing">
Text Message 
<div class="ch">ch 1</div>
<div class="ch">ch 2</div>
<div class="ch">ch 3</div>
<div class="ch">ch 4</div>
</div>

jquery:

$(document).on('click','.ch',function(){
   $(this).remove();
});

jsfiddle

The below solution works only if your mark up is as exactly as you have given.

//This solution will not work if the .ch element has another class or has another descendant element
document.querySelector('#listing').addEventListener('click', function(e) {
  if (e.target.className = 'ch') { //or if(e.target.classList.contains('ch')){
    this.removeChild(e.target)
  }
})
<div id="listing">
  Text Message
  <div class="ch">ch 1</div>
  <div class="ch">ch 2</div>
  <div class="ch">ch 3</div>
  <div class="ch">ch 4</div>
</div>

Another choice is to iterate over all the ch elements and then register a click handler to it

发布评论

评论列表(0)

  1. 暂无评论