Select 一个元素里面没有某些元素

Select an element without certain elements inside

我想通过单击 div 中的按钮将我的 HTML div 导出为 jpeg,但我不想打印该按钮。因此,我需要一个 selector 在 div 上,它不 select 带有 div.

内的某个 class 的项目

我试过 :not() select或者,但它似乎不起作用:

$(".asdf:not(.export-button)")

这是我的代码:

<div id="asdf">
    <p>Lorem ipsum ...</p>
    <img src="area_graph.png" alt="graph"/>
    <p>hallo</p>
    <button class="export-button">EXPORT</button>
</div>

$(".export-button").on("click", function () {

    var $tempNode = $(this).parent()[0];
    $tempNode.remove(".export-button");
    console.log($tempNode);

    // image export
});

因为 DOM 是一个树结构,当您 select 一个元素时,您会得到它的所有子元素。要执行您需要的操作,您需要 select 元素然后 remove() 在单独的语句中不想要的元素

$(".export-button").on("click", function() {
  var $tempNode = $(this).parent().clone();
  $tempNode.find(".export-button").remove();
  console.log($tempNode.html());

  // image export
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="asdf">
  <p>Lorem ipsum ...</p>
  <img src="area_graph.png" alt="graph" />
  <p>hallo</p>
  <button class="export-button">EXPORT</button>
</div>