本文介绍了如何检测哪个< td>被点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:
我有一个和一些和。我需要检测哪些被点击(最终得到Id)。我已经构建了以下作为参考。


$ b $ (函数()){
$(。table)。find(tr)。click(function(){
alert(< tr> clicked);
var td2 = $(this).find(。td2:first)。text();
alert(td2);
});
});

我有一个.click()事件,点击时我正在做一些动作,但我需要检测是否单击了特定的< td> 以排除该TD。
基本上,当任何被点击的时候,应该做一些动作(除非点击了一个特定的< td> ,在这种情况下不应该做任何事情)



您认为什么? 如果您试图应用特定行动到 td 标签,除了它们包含特定的类时,您可以使用:not 来排除该类。您还可以将您的点击事件直接应用于 td :

  $('td:not(.td2)')。click(function(){
var clickedCell = $(this).text();
alert(clickedCell);
});


I have the following case:I have a with some and . I need to detect which was clicked (eventually get the Id). I have build the following JS Fiddle as a reference.

jQuery(document).ready(function() {
    $(".table").find("tr").click( function(){
        alert("<tr> clicked");
        var td2 = $(this).find(".td2:first").text();
        alert(td2);
    });
});

I have a .click() event and I am doing some actions when is clicked but I need to detect if a specific <td> is clicked in order to exclude that TD.Basically when any is clicked some actions should be done (unless a specific <td> is clicked, and in that case nothing should be done)

What do you think?

解决方案

If you are trying to apply a specific action to td tags except for when they contain a specific class, you can exclude the class with :not. You can also apply your click event directly to the td: JS Fiddle

$('td:not(.td2)').click(function () {
    var clickedCell = $(this).text();
    alert(clickedCell);
});

这篇关于如何检测哪个&lt; td&gt;被点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:25