jQuery选择除一个元素外的所有元素

时间:2017-03-22 15:40:48

标签: javascript jquery html jquery-selectors

我有html,如下所示,并希望选择除override def receive: Receive = { case StringSet(p) => // } 元素之外的所有内容。最终目标是有两个可点击的区域,每个区域都做不同的事情。但我无法将较大区域与<a>元素隔离开来。

<a>

所以可以选择所有的&#39;除了那个&#39; a&#39;在最后一次&#39;?

我尝试过像<td title="title1" class="class1"> <p class="pclass1">value1</p></td> <td> <p class="someClass1" title="someTitle1">someValue1</p> <p class="someClass2" title="someTitle2">someValue2</p></td> <p class="someClass3" title="someTitle3">someValue3</p> </td> <td><p title="otherTitle1" class="otherClass1">otherValue1</p></td> <td><p title="otherTitle2" class="otherClass2">otherValue2</p></td> <td><p title="otherTitle3" class="otherClass3">otherValue3</p></td> <td><p title="otherTitle4" class="otherClass4">otherValue4</p></td> <td title="title2" class="class2"><a href="#" class="no-select">don't select me</a></td> 这样的事情没有成功

2 个答案:

答案 0 :(得分:1)

执行此操作的最简单方法是将单击处理程序委派给公共父tr元素,然后检测引发事件的元素并运行相应的逻辑。像这样:

&#13;
&#13;
$('tr').click(function(e) {
  if ($(e.target).is('.no-select')) {
    console.log('link clicked...');
  } else {
    console.log('row clicked...');
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td title="title1" class="class1"><p class="pclass1">value1</p></td>
    <td>
      <p class="someClass1" title="someTitle1">someValue1</p>
      <p class="someClass2" title="someTitle2">someValue2</p>
    </td>
    <td><p class="someClass3" title="someTitle3">someValue3</p></td>
    <td><p title="otherTitle1" class="otherClass1">otherValue1</p></td>
    <td><p title="otherTitle2" class="otherClass2">otherValue2</p></td>
    <td><p title="otherTitle3" class="otherClass3">otherValue3</p></td>
    <td><p title="otherTitle4" class="otherClass4">otherValue4</p></td>
    <td title="title2" class="class2"><a href="#" class="no-select">don't select me</a></td>
  </tr>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

一种方法是检查事件中target元素的类名。

$("td").on("click", function(e) {

  if (e.target.className === 'no-select') {
    return;
  }

  // Now we can do something here because we know
  // the event wasn't triggered by a `no-select` element
});