简体   繁体   English

从列表 jquery 中获取最接近的 class

[英]Get the closest class from a list jquery

It's hard to explain, so I created an example:很难解释,所以我创建了一个示例:

jsfiddle jsfiddle

My idea is to change the color of each column when the respective input is in action...我的想法是在相应的输入生效时更改每列的颜色......

If anyone has a better idea to do this - please let me know!如果有人对此有更好的想法 - 请告诉我!

When I focus the input, I need the current class of the column.当我聚焦输入时,我需要该列的当前 class。

  • first column input, get the class of the RED column第一列输入,得到RED列的class
  • and the second one, get the class of the BLUE column第二个,得到BLUE列的class
  • and so go's on...所以继续...

Because if I get the class, then I can manipulate anything with this class.因为如果我得到 class,那么我可以用这个 class 操作任何东西。

the code is here:代码在这里:

$(".inputTest").focusin(function(){
    var class = $(this).closest('.tableList')
                    .children().children().children('.auxClass')
                    .attr('class')
                    .split(' ')[0];
                alert(class);
});

This is the main code, I try alot of stuffs to get, but nothing.这是主要代码,我尝试了很多东西,但没有。

Thanks谢谢

First I'd add an outer table to split the page in a left and a right hand side.首先,我将添加一个外部表格以将页面拆分为左侧和右侧。 That way, the inputs below the red border and the inputs below the blue border each have their own table.这样,红色边框下方的输入和蓝色边框下方的输入都有自己的表格。

Then you can search for the first td below the closest table:然后你可以在最近的表下面搜索第一个td

$(".inputTest").focusin(function(){
    var class = $(this).closest('table').find('td:eq(0)').attr('class');
    alert(class);
});

Click for working jsfiddle example.单击工作 jsfiddle 示例。

Try this:尝试这个:

$(".inputTest").focus(function(){
    var class = $(this).closest('table').parent().attr('class');
    alert(class);
});

Edit: Oh, i just realised your inputs are not inside your tables, i think you're gonna have a hard time matching them up to the table/column they're under then.编辑:哦,我刚刚意识到您的输入不在您的表格内,我认为您将很难将它们与它们所在的表格/列匹配。 You'd need to add a common attribute to identify them by.您需要添加一个通用属性来识别它们。

As mentioned in other answers your inputs are not actually in the same "columns" as your red/blue bordered tables, but you can make it so they are using the <col> element on the main table, then using the index value you can match your inputs to their column正如其他答案中提到的,您的输入实际上与您的红色/蓝色边框表不在同一个“列”中,但您可以这样做,以便他们使用主表上的<col>元素,然后使用您可以使用的索引值将您的输入与他们的列相匹配

Working Example工作示例


HTML - the only addition is the two <col> elements at the start HTML - 唯一添加的是开头的两个<col>元素

<table width="100%" border="1" class='tableList'>
<col span="2" class="left">
<col span="2" class="right">

    <tr>
        <td class="101 auxClass" width="261px" colspan="2" style="border: solid red;">
            <table border="1" cellspacing="1" cellpadding="1" width="100%" height="70px">
                <tbody>
                    <tr>
                        <td colspan="2">Something</td>
                    </tr>
                    <tr>
                        <td width="78px">Something 2</td>
                        <td>Total</td>
                    </tr>
                </tbody>
            </table>
        </td>
        <td class="102" width="261px" colspan="2" style="border: solid blue;">
            <table border="1" cellspacing="1" cellpadding="1" width="100%" height="70px">
                <tbody>
                    <tr>
                        <td colspan="2">
                       Something 3
                        </td>
                    </tr>
                    <tr>
                        <td width="78px">Something 4</td>
                        <td width="75px">Total 2</td>
                    </tr>
                </tbody>
            </table>
        </td>
    </tr>
    <tr>
        <td>Result</td>
        <td><input type="text" class="inputTest"/></td>
        <td>Result</td>
        <td><input type="text" class="inputTest"/></td>
    </tr>
    <tr>
        <td>Result</td>
        <td><input type="text" class="inputTest"/></td>
        <td>Result</td>
        <td><input type="text" class="inputTest"/></td>
    </tr>
    <tr>
        <td>Result</td>
        <td><input type="text" class="inputTest"/></td>
        <td>Result</td>
        <td><input type="text" class="inputTest"/></td>
    </tr>
</table>

CSS: CSS:

col.current {background: #eee;}

jQuery jQuery

$(".inputTest").focusin(function(){
    var colidx = $(this).closest('td').index();


     if (colidx == 1) {
          $("col").removeClass('current');
          $("col.left").addClass('current');
     }  else if (colidx == 3) {
          $("col").removeClass('current');   
          $("col.right").addClass('current');
     }

});

Your main table is actually 4 columns, and you need to split it into two halfs of two columns each with the input being in the second column of each half您的主表实际上是 4 列,您需要将其分成两列的两半,每列的输入位于每半的第二列

The jQuery is finding the index of the parent td of the input - there are four columns in the main table so the index of a td will either be 0,1,2 or 3 - and the input is either going to be in cell index 1 or cell index 3. When it finds out which one it add a class to the relevant col element to which you can add a background highlight.. jQuery 正在查找输入的父td的索引 - 主表中有四列,因此td的索引将是 0、1、2 或 3 - input将在单元格索引中1 或单元格索引 3。当它找到哪一个时,它会将 class 添加到相关的col元素中,您可以在其中添加背景突出显示。

Note though that the CSS you can apply to a col element is limited, see: http://www.quirksmode.org/css/columns.html , for the options so it would depend what you want to do请注意,尽管您可以应用于col元素的 CSS 是有限的,但请参阅: http://www.quirksmode.org/css/columns.html取决于您想要做什么,所以它的选项

however I think from this you could probably target td index 0 & 1, or td index 2 & 3 if needed但是我认为,如果需要,您可能可以针对 td 索引 0 和 1 或 td 索引 2 和 3

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

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