【问题标题】:How to know element clicked within a table?如何知道在表格中点击的元素?
【发布时间】:2013-09-15 02:19:21
【问题描述】:

我正在尝试在 HTML 表格中的 TR 中单击该元素。如果我单击 TR 内的 Select 输入,CurrentTarget 字段返回“TR”,OriginalTarget 返回“SELECT”。

这是我的 HTML:

<table id="0" class="tableEdit">
    <thead>
        <tr>
            <th name="id"></th>
            <th name="name">Descripción Registro</th>
            <th name="select">Fecha</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1651</td>
            <td>Name</td>
            <td>
                <select name="selectName">
                    <option value="1">1</option>
                    <option value="2">2</option>
                </select>
            </td>
        </tr>
    </tbody>
</table>

这是我的代码:

            //trb is each TR element of the line
    $(trb).click(function(elem){
        if (elem.currentTarget && elem.currentTarget.tagName.toLowerCase() === "tr" && !isInput(elem.originalTarget)){
            if (editableRow){
                var rowTrigger = editableRow.find("button").get();
                $.editRow(rowTrigger,$.tableEditor.vault.getTableID($("#" + id)));
            }
    });

此代码在我的网络浏览器上运行良好,但在移动设备上却不行,因为 OriginalTarget 返回 undefined。有没有办法在移动网络浏览器上获取原始目标?

【问题讨论】:

标签: javascript jquery html mobile


【解决方案1】:

您实际上并没有说出trb 是什么,但听起来它可能是您表中的一组tr 元素。

您要查找的是elem.target。这是被点击的最顶层元素,启动事件的元素。 (FWIW,我不会调用传递给事件处理程序elem 的参数,它是一个事件,而不是一个元素。)

例如,如果您有:

<table>
<tbody>
<tr>
<td><span><strong>Click me</strong></span></td>
</tr>
</tbody>
</table>

...还有这个:

$("tr").click(function(e) {
    console.log(e.target.tagName);
});

...然后你点击文本“点击我”,你会看到

强大

...在控制台中。


旁注:如果您想知道单击了哪个单元格或行,可以方便地使用closest,例如:

var $td = $(e.target).closest('td');

【讨论】:

  • 被点击的元素是currentTarget,不是吗?
  • @Cherniv:不,currentTargetthis 相同(通常)。它指的是处理程序绑定到的元素。
  • @Cherniv:不,是target。所有血腥细节:w3.org/TR/DOM-Level-3-Events/#events-event-type-target
  • @FelixKling 好的,这就是他的 currentTarget 返回 tr 的原因,因为他是这样绑定的:$(trb).click(...
  • @T.J.Crowder 这两个名字很混乱,更直观的认为currentTarget是一个“最顶层元素”,不是吗?
【解决方案2】:

要正确理解,您需要了解 javascript 的基础知识。

大多数浏览器,尤其是像移动设备这样的现代浏览器都使用标准 javascript,例如:

element.addEventListener //to add Event Handlers
//those eventListeners return always the event as first parameter
//and this event contains the target which can be called with
event.target

但较旧的浏览器或 Internet Explorer 使用不同的方式来实现这一点

attachEvent //to add eventListener
// the event needs to be called with
window.event
// and the target is called
event.srcElement

知道可以这样写函数:

//addEvent checks if addEventListener exists else it uses attachEvnet
//as you can see attachEvent also has only 2 parameters and needs a 'on'
//before the event name
function addEvent(a,e,f){//Element,Event,Function(the actual eventHandler)
 window.addEventListener?a.addEventListener(e,f,false):a.attachEvent('on'+e,f);
}

//handler handles in this case the click event
//it checks if the first parameter is event else it uses the window.event
//it checks if inside the event exists a event.target else event.srcElement
//then it loops through the parentNode until it finds(this case) the TR Element
//and just to test it alerts the content of it
//if you want to get the TD element replace e.target.parentNode with e.target
//and TR with TD
// so you get the proper row or column clicked.
function handler(e){
 e=e||window.event;
 e.target=e.target||e.srcElement;
 var x=e.target.parentNode;
 while(x.nodeName!='TR'){//or x.localName but thats lowercase 'tr'
  x=x.parentNode;
 }
 alert(x.innerHTML);
}

//when the page loads it it searches for the first table (note the [0])
//and adds a eventListener to the whole table.
//this allows you to have one eventListener on the full table but
//control every single row or column.
window.onload=function(){
 var table=document.getElementsByTagName('table')[0];
 addEvent(table,'click',handler);
}

这就是 jQuery 存在的原因......以避免所有这些双重检查。

无论如何......经过一些测试并且移动浏览器支持现代标准方式......我更喜欢从移动网络应用程序中省略 jQuery,因为它只会减慢一切。

所以对于我使用的移动设备:

function handler(e){
 var x=e.target;
 while(x.nodeName!='TR'){
  x=x.parentNode;
 }
 console.log(x.innerHTML);
}
window.onload=function(){
 var table=document.getElementsByTagName('table')[0];
 table.addEventListener('click',handler,false);
}

【讨论】:

    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    相关资源
    最近更新 更多