JQuery Exclude Child元素

时间:2018-03-13 06:28:34

标签: jquery html

我正在使用html的dom对象如下:

function init() {
  var images = [
    'https://www.bryanrock.com/application/files/7914/3349/4593/rock.png',
    'https://images-na.ssl-images-amazon.com/images/I/81tAWzf0iKL._SY450_.jpg',
    'https://cdn1.creativememories.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/s/c/scissors1.jpg',
    'http://animals.net/wp-content/uploads/2017/02/Lizard-2-640x425.jpg',
    'http://www.tabletmag.com/wp-content/files_mf/spock62037.jpg'
  ];
  var image = document.getElementById('image');

  setInterval(function() {
    var rnd = Math.floor(Math.random() * 5);
    image.src = images[rnd];
    image.onload = function() {
      var nw = image.naturalWidth;
      var nh = image.naturalHeight;

      if (nw > nh) {
        image.width = 300;
        image.height = 300 * nh / nw;
      }
      else {
        image.height = 300;
        image.width = 300 * nw / nh;
      }
    };
  }, 1000);
}

基本上我想删除所有没有jquery的子div。 我试过了,

while(column_name(1) = like'%box')
    begin
    select @value = column_name  from table 
    select column_name = column_name + 1 (i.e select next column name from table)
    end

但是上面的代码只返回文本而不是我想要的文本。 我想得到如下结果:

<div class="parent">
    this is my <span>11</span> original text i <span>23</span> want
    <div class="child1">Lorem Ipsum is simply dummy text</div>
    <div class="child2">Lorem Ipsum is simply dummy text</div>
</div>

6 个答案:

答案 0 :(得分:1)

在父类中找到div并删除。

&#13;
&#13;
$('.parent').find('div').remove();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
    this is my <span>11</span> original text i <span>23</span> want
    <div class="child1">Lorem Ipsum is simply dummy text</div>
    <div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
&#13;
&#13;
&#13;

OR:

&#13;
&#13;
   $(document).find('.parent > div').remove();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
    this is my <span>11</span> original text i <span>23</span> want
    <div class="child1">Lorem Ipsum is simply dummy text</div>
    <div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

试试这个

$('.parent > div').remove();

答案 2 :(得分:1)

使用 :not(span) 选择器选择不跨越的所有内容。

&#13;
&#13;
$('.parent').find(':not(span)').remove();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
    this is my <span>11</span> original text i <span>23</span> want
    <div class="child1">Lorem Ipsum is simply dummy text</div>
    <div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

只需使用.parent div Jquery选择器选择具有类div的所有parent内部元素,然后使用remove()将其从DOM中删除:

&#13;
&#13;
$(document).ready(function(){
  $(".parent div").remove();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
    this is my <span>11</span> original text i <span>23</span> want
    <div class="child1">Lorem Ipsum is simply dummy text</div>
    <div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:1)

$(".parent > [class^=child]").remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
    this is my <span>11</span> original text i <span>23</span> want
    <div class="child1">Lorem Ipsum is simply dummy text</div>
    <div class="child2">Lorem Ipsum is simply dummy text</div>
</div>

答案 5 :(得分:0)

如果要选择范围文本:

$(".parent span").text();

$(".parent").children("span").text();

如果你想选择除了跨度之外的其他人(也许隐藏它们):

$(".parent").find("div") //.hide() or .remove()