简体   繁体   English

如何使用 jquery 从 div 部分删除特定值

[英]How to remove specific value from div section using jquery

I have created a div where it contain dynamic labels.我创建了一个包含动态标签的 div。 I am trying to remove specific value from div section using jquery.I have declared variable which conatin the string if the string matches in the div section string it should get removed from div.我正在尝试使用 jquery 从 div 部分删除特定值。我已经声明了包含字符串的变量,如果字符串在 div 部分字符串中匹配,它应该从 div 中删除。 Is there any way to to do that?有没有办法做到这一点?

      <div class="VS" id="MM" style="border:1px solid gray; width:150px;">
      <label class="custom_label_div2" id="lbl1"> XYZ </label>
      <label class="custom_label_div2" id="lbl2"> XYZC </label>
      <label class="custom_label_div2" id="lbl3"> QWER </label>
      <div>

I was trying to do something like this我试图做这样的事情

    var str = XYZ; --- this is my varibale which will contain any label deriving from somewhere else it can contain any one of the value i.e. XYZ or XYZC or QWER
    $('#MM').each(function() {
       var $this = $(this);
       if ($this.text() === str) {
           $this.remove();
       }
    });
if ($this.text() === str) {
           $this.remove();
       }
  • this is removing the element itself, if you need to empty it use :这是删除元素本身,如果您需要清空它,请使用:
if ($this.text() === str) {
           $this.text("");
       }

or或者

if ($this.text() === str) {
           $this.html(""); // if there are other elements inside
       }

or just要不就

if ($this.text() === str) {
           $this.empty();
       }

At the moment Your JQuery string finds only the div with id "MM", but You want the child elements so try this:目前,您的 JQuery 字符串仅找到 ID 为“MM”的 div,但您想要子元素,因此请尝试以下操作:

var str = XYZ;
$('#9MMM > label').each(function() {
    var $this = $(this);
    if ($this.text() === str) {
        $this.remove();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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