[英]What is the appropriate selector for :not $(this)
The above yellow link does not provide a solutions to this. 上面的黄色链接没有为此提供解决方案。 The correct answer is .siblings()
which is not in any of the linked solutions 正确的答案是.siblings()
,它不在任何链接的解决方案中
I am trying to select all td
elements except for the one passed as $(this)
. 我试图选择所有的td
元素,除了以$(this)
传递的元素。 What is the proper way to do this ? 这样做的正确方法是什么?
I don't think the following works : 我认为以下作品无效:
$(this).find(':not')
..since I am looking for all at the same DOM level as $(this). ..因为我正在寻找与$(this)相同的DOM级别的所有对象。
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('a').click(function() { alert($(this).text()); }); }); </script> </head> <body> <a>Apples</a><br /> <a>Bananas</a><br /> <a>Oranges</a><br /> </body> </html>
Just stating this to be clear that I am looking for a general solution, not one specific to this example. 只是为了清楚表明我正在寻找一种通用解决方案,而不是针对此示例的解决方案。 :) :)
Use the .not()
method to exclude a previous jQuery selection. 使用.not()
方法排除先前的jQuery选择。 You can pass a selector to it, but also an existing element or jQuery object. 您可以将选择器传递给它,还可以传递一个现有元素或jQuery对象。
var others = $('td').not(this);
$('td').on('click', function() { $(this).css('color', 'red') $('td').not(this).css('color', 'blue'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><td>Cell1</td><td>Cell2</td></tr> <tr><td>Cell3</td><td>Cell4</td></tr> <tr><td>Cell5</td><td>Cell5</td></tr> </table>
jQuery.not() reference jQuery.not()参考
After seeing your update and the various comments, I start to think that you just want to get the siblings of this
. 在看到您的更新和各种评论之后,我开始认为您只是想获得this
的兄弟姐妹。 If you just want an element's siblings, use jQuery's siblings() function. 如果只需要元素的同级,请使用jQuery的siblings()函数。 This function optionally accepts a selector to get only specific siblings. 该函数可以选择接受选择器以仅获取特定的同级。 Anyway, siblings implicitly excludes this
itself. 无论如何,兄弟姐妹暗中排除了this
本身。
$('td').on('click', function() { $(this).css('color', 'red') $(this).siblings().css('color', 'blue'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><td>Cell1</td><td>Cell2</td></tr> <tr><td>Cell3</td><td>Cell4</td></tr> <tr><td>Cell5</td><td>Cell5</td></tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.