[英]How to use click in table cell except click on a select box in jQuery
have a table which containing some images and some select boxes .I want to highlight row whenever user click on that row(means any td) except select boxes and images 有一个包含一些图像和一些选择框的表。我想在用户单击该行时突出显示该行(表示td),但选择框和图像除外
Now i am using this, 现在我在用这个
$("selector").live("click",function() {
$(this)
.parents("tr:first")
.toggleClass("diffColor")
;
});
But it is applying the class if i click on select box or image.But i dont need when i click on select box or any other images. 但是如果我单击选择框或图像,它将正在应用该类。但是当我单击选择框或任何其他图像时我不需要。
see here 看这里
Thanks in advance... 提前致谢...
Try this: 尝试这个:
$(".selector").on("click", function (e) {
if (e.target === this) $(this).parent("tr:first").toggleClass("diffColor");
});
Assumming the fact that .selector
is the class name for all the td
假设
.selector
是所有td
的类名
Here, this
means the element in the current scope, which is always the td
clicked here. 在这里,
this
表示当前作用域中的元素,始终是此处单击的td
。
and e.target
means the element actually clicked, which can be td
or checkbox
or anything inside the td
. e.target
表示实际单击的元素,可以是td
或checkbox
或td
内部的任何内容。
So, in case the element actually clicked is not the td
in the current scope, e.target === this
return false and nothing happens (no click event is fired) and vice-versa. 因此,如果实际单击的元素不是当前作用域中的
td
,例如e.target === this
返回false并且不发生任何事情(不会触发click事件),反之亦然。
If you don't want to toggleClass
when clicking on a td
which contains an <img>
or <select>
you can do the following: 如果你不想
toggleClass
上点击时, td
其中包含<img>
或<select>
您可以执行以下操作:
$('selector').live('click', function() {
if (!$(this).has('select, img').length) {
$(this).parents('tr:first').toggleClass('diffColor');
}
});
I have solution with checking if the TD contains image or select box. 我有检查TD是否包含图像或选择框的解决方案。
$("#tableId td").on("click",function() {
var ths=$(this);
if((ths.has("select").length ? false : true)&&(ths.has("img").length ? false : true)){
$(this).parent().toggleClass("diffColor");
}
});
will this be ok for you? 这样可以吗 just add class "change" to td.
只需将类“ change”添加到td。
$("#tableId td.change").on("click",function() {
$(this)
.parents("tr:first")
.toggleClass("diffColor")
;
});
<table id="tableId" border="1">
<thead>
<th></th>
<th>First</th>
<th>Second</th>
<th>Third</th>
</thead>
<tbody>
<td><input type="checkbox"/></td>
<td class="change">FirstTableColumn1</td>
<td class="change">FirstTableColumn2</td>
<td><select>
<option>AAA</option>
<option>BBB</option>
<option>CCC</option>
<option>DDD</option>
</select></td>
</tbody>
</table>
This happens because your events are bubbling up. 发生这种情况是因为您的事件正在冒泡。
you can add an event.stopPropagation for the click events on the children to prevent this. 您可以为子级事件中的click事件添加event.stopPropagation来防止这种情况。
so for any input and select elements add the following to prevent propagation. 因此,对于任何输入和选择元素,请添加以下内容以防止传播。 if you already have event handlers for the clicks, add stopPropagation to the beginning of your functions.
如果您已经具有单击的事件处理程序,则将stopPropagation添加到函数的开头。
$("#tableId").find("input, select")
.click(function(e){e.stopPropagation();});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.