简体   繁体   English

仅在单击第5行元素或第3行元素中的文本框超出焦点时,才使用JQuery突出显示表行

[英]Using JQuery to highlight table row only when clicking on 5th row-element or outfocussing of textbox in 3th row-element

I have a table where rows can be highlighted once a row is clicked or a textbox is left. 我有一张桌子,单击某行或保留一个文本框后,便可以突出显示行。

My table is called "editTable" and the body of the table looks like this (rows are dynamically generated in a foreach loop so there can be more than one): 我的表称为“ editTable”,表的主体如下所示(行在foreach循环中动态生成,因此可以有多个行):

<tbody>
    <tr>
        <td>
           blabla
        </td>
        <td>
            blabla
        </td>
        <td>
           <input type="submit" id="item_Unitprice" />
       </td>
       <td id="total"></td>
       <td id="highlighter">
           <img src="~/Content/images/highlight.gif" /></td>
    </tr>
</tbody>

And the table highlighting works as follows: 突出显示的表的工作方式如下:

// Highlighting table
$('#editTable').on('click focusout', 'tbody tr', function (event) {
    $(this).toggleClass('highlight');
});

This works, but now I want the table highlighting only to respond to a click on the 5th td-element in the rows (ie the highlight image) or when leaving the textbox in the 3th td-element for each row. 这行得通,但是现在我希望突出显示的表格仅对行中第5个td元素(即突出显示图像)的单击做出响应,或者在每行的第3个td元素中保留文本框时做出响应。

Is this possible and if so, how? 这可能吗?如果可以,怎么办?

如何将$('#editTable')更改$('#editTable').eq(4)

Alright I found a solution by doing the following things: 好吧,我通过执行以下操作找到了解决方案:

(1) for highlighting on focusout I still use the old function: (1)为了突出聚焦,我仍然使用旧功能:

// Highlighting table on focusout
$('#editTable').on('focusout', 'tbody tr', function (event) {
    $(this).toggleClass('highlight');
});

(2) for highlighting on a click I add a onclick event to the image like this: (2)为了突出显示单击,我向图像添加了onclick事件,如下所示:

<img src="~/Content/images/highlight.gif" onclick="highlight(this)" /> 

and add the following code to my JavaScript file (based on this question for deleting a row after a click): 并将以下代码添加到我的JavaScript文件中(基于问题,单击后删除一行):

function highlight(o) {
    var p = o.parentNode.parentNode;
    $(p).toggleClass('highlight');
}

This makes it work. 这使其工作。 Of course it would be prettier to combine the two into one but for now I am happy with the functionality. 当然,将两者结合起来会更漂亮,但是现在我对功能感到满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM