Javascript 如何使用:not()选择器(包括父元素:第一个子元素)
我有一个JQuery函数,它使tr元素成为超链接,但是,当单击td之外的任何子元素时,它不应该触发。我还想排除第一个td元素,因为它包含一个复选框 我尝试过这个方法,但它不允许任何操作,如复选框、子链接和子javascript:Javascript 如何使用:not()选择器(包括父元素:第一个子元素),javascript,jquery,Javascript,Jquery,我有一个JQuery函数,它使tr元素成为超链接,但是,当单击td之外的任何子元素时,它不应该触发。我还想排除第一个td元素,因为它包含一个复选框 我尝试过这个方法,但它不允许任何操作,如复选框、子链接和子javascript: $('table.table-striped tr[data-href] td').click(function (e) { e.stopPropagation(); var $parent = $(this).parents('tr'); wi
$('table.table-striped tr[data-href] td').click(function (e) {
e.stopPropagation();
var $parent = $(this).parents('tr');
window.location = $parent.data('href');
}).children().click(function(e) {
return false;
});
然后我尝试了这个,但它似乎根本不起作用:
$(this).on('click', 'table.table-striped tr[data-href] td:not(:first-child):not(a.codelink):not(.actions):not(.dropdown-toggle):not(.dropdown):not(.caret):not(input)', function (e) {
e.stopPropagation();
var $parent = $(this).parents('tr');
window.location = $parent.data('href');
});
以下是表格行的示例:
<tr class="datarow" data-href="/stock/675144">
<td><span class="largercheckbox"><input type="checkbox" class="selectrow" id="chk675144" data-id="675144"></span></td>
<td><div class="stockindentifier">test</div></td>
<td><a href="/link" class="codelink grey">1011.3</a></td>
<td class="actions">
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" data-target="#" href="#"><b class="caret"></b></a>
<ul class="dropdown-menu pull-right"><li><a href="/stock/edit/675144"><i class="icon-edit"></i> Edit</a></li><li><a href="/stock/delete/675144" data-confirm="true" rel="nofollow"><i class="icon-trash"></i> Delete</a></li></ul>
</div>
</td>
测试
使用is()确保单击的是td元素,以防止操作在任何子元素上运行:
$('table.table-striped tr[data-href] td').click(function (e) {
if ($(e.target).is('td'))
{
e.stopPropagation();
var $parent = $(this).parents('tr');
window.location = $parent.data('href');
}
});
:not()
只对应用它的元素起作用,因此td:not(输入)
没有什么意义。您需要查看事件委派,在事件委派中,您可以捕获对td
的所有点击,并使用$(e.target).is('td')
或类似的内容进行筛选。@Hereticsmonkey谢谢,我在您的帮助下的最终工作解决方案如下。如果您在“:not selector”之间使用额外的空间,它将按照预期工作,比如:“table.table-striped tr[data href]td:not(:first child):not(a.codelink):not(.actions)…”等等。因为类不在td标记元素本身内部。