使用JQuery查找下一个表行

时间:2022-08-29 22:15:16

Using JQuery, how do you bind a click event to a table cell (below, class="expand") that will change the image src (which is in the clicked cell - original will be plus.gif, alternating with minus.gif) and hide/show the row immediately below it based on whether that row has a class of "hide". (show it if it has a class of "hide" and hide if it does not have a class of "hide"). I am flexible with changing ids and classes in the markup.

使用JQuery,如何将click事件绑定到表单元格(下面,class =“expand”),这将更改图像src(在单击的单元格中 - 原始将是plus.gif,与minus.gif交替)并根据该行是否具有“隐藏”类隐藏/显示其下方的行。 (如果它有一个“隐藏”类,则显示它,如果它没有“隐藏”类则隐藏)。我可以灵活地更改标记中的ID和类。

Thanks

Table rows

<tr>
 <td class="expand"><img src="plus.gif"/></td>
 <td>Data1</td><td>Data2</td><td>Data3</td>
</tr>
<tr class="show hide">
 <td> </td>
 <td>Data4</td><td>Data5</td><td>Data6</td>
</tr>

5 个解决方案

#1


18  

You don't need the show and hide tags:

您不需要显示和隐藏标签:

$(document).ready(function(){   
    $('.expand').click(function() {
        if( $(this).hasClass('hidden') )
            $('img', this).attr("src", "plus.jpg");
        else 
            $('img', this).attr("src", "minus.jpg");

        $(this).toggleClass('hidden');
        $(this).parent().next().toggle();
    });
});

edit: Okay, I added the code for changing the image. That's just one way to do it. I added a class to the expand attribute as a tag when the row that follows is hidden and removed it when the row was shown.

编辑:好的,我添加了更改图像的代码。这只是一种方法。当隐藏后面的行时,我在expand属性中添加了一个类作为标记,并在显示行时将其删除。

#2


9  

Nobody has any love for the ternary operator? :) I understand readability considerations, but for some reason it clicks for me to write it as:

没有人对三元运营商有任何爱好? :)我理解可读性考虑因素,但出于某种原因,我点击它将其写为:

$(document).ready( function () {
    $(".expand").click(function() {
            $("img",this).attr("src", 
                 $("img",this)
                    .attr("src")=="minus.gif" ? "plus.gif" : "minus.gif"
            );
            $(this).parent().next().toggle();
    });
});

...and has the benefit of no extraneous classes.

......并且没有任何无关的课程。

#3


3  

I had to solve this problem recently, but mine involved some nested tables, so I needed a more specific, safer version of javascript. My situation was a little different because I had contents of a td and wanted to toggle the next TR, but the concept remains the same.

我最近不得不解决这个问题,但我的涉及一些嵌套表,所以我需要一个更具体,更安全的javascript版本。我的情况有点不同,因为我有一个td的内容,想要切换下一个TR,但概念保持不变。

$(document).ready(function() {
    $('.expandButton').click(function() {
        $(this).closest('tr').next('tr.expandable').fadeToggle();
    });
});

Closest grabs the nearest TR, in this case the first parent. You could add a CSS class on there if you want to get extremely specific. Then I specify to grab the next TR with a class of expandable, the target for this button. Then I just fadeToggle() it to toggle whether it is displayed or not. Specifying the selectors really helps narrow down what it will handle.

最近的TR抓住最近的TR,在这种情况下是第一个父。如果你想要非常具体,你可以在那里添加一个CSS类。然后我指定使用可扩展类(此按钮的目标)来获取下一个TR。然后我只是fadeToggle()它来切换它是否显示。指定选择器确实有助于缩小它的处理范围。

#4


1  

Try this...

//this will bind the click event
//put this in a $(document).ready or something
$(".expand").click(expand_ClickEvent);

//this is your event handler
function expand_ClickEvent(){
   //get the TR that you want to show/hide
   var TR = $('.expand').parent().next();

   //check its class
   if (TR.hasClass('hide')){
      TR.removeClass('hide'); //remove the hide class
      TR.addClass('show');    //change it to the show class
      TR.show();              //show the TR (you can use any jquery animation)

      //change the image URL
      //select the expand class and the img in it, then change its src attribute
      $('.expand img').attr('src', 'minus.gif');
   } else {
      TR.removeClass('show'); //remove the show class
      TR.addClass('hide');    //change it to the hide class
      TR.hide();              //hide the TR (you can use any jquery animation)

      //change the image URL
     //select the expand class and the img in it, then change its src attribute
      $('.expand img').attr('src', 'plus.gif');
   }
}

Hope this helps.

希望这可以帮助。

#5


-2  

This is how the images are set up in the html

这是在html中设置图像的方式

<tr>

<td colspan="2" align="center"
<input type="image" src="save.gif" id="saveButton" name="saveButton"
    style="visibility: collapse; display: none" 
     onclick="ToggleFunction(false)"/>

<input type="image" src="saveDisabled.jpg" id="saveButtonDisabled" 
      name="saveButton" style="visibility: collapse; display: inline"
      onclick="ToggleFunction(true)"/>
</td>
</tr>

Change the onClick event to your own function that's in JS to toggle between them.

将onClick事件更改为您自己的JS函数,以便在它们之间切换。

In the

ToggleFunction(seeSaveButton){    
    if(seeSaveButton){
        $("#saveButton").attr("disabled", true)
                        .attr("style", "visibility: collapse; display: none;");
        $("#saveButtonDisabled").attr("disabled", true)
                                .attr("style", "display: inline;");
    }    
    else {    
        $("#saveButton").attr("disabled", false)
                        .attr("style", "display: inline;");
        $("#saveButtonDisabled")
                        .attr("disabled", true)
                        .attr("style", "visibility: collapse; display: none;");
    }
}

#1


18  

You don't need the show and hide tags:

您不需要显示和隐藏标签:

$(document).ready(function(){   
    $('.expand').click(function() {
        if( $(this).hasClass('hidden') )
            $('img', this).attr("src", "plus.jpg");
        else 
            $('img', this).attr("src", "minus.jpg");

        $(this).toggleClass('hidden');
        $(this).parent().next().toggle();
    });
});

edit: Okay, I added the code for changing the image. That's just one way to do it. I added a class to the expand attribute as a tag when the row that follows is hidden and removed it when the row was shown.

编辑:好的,我添加了更改图像的代码。这只是一种方法。当隐藏后面的行时,我在expand属性中添加了一个类作为标记,并在显示行时将其删除。

#2


9  

Nobody has any love for the ternary operator? :) I understand readability considerations, but for some reason it clicks for me to write it as:

没有人对三元运营商有任何爱好? :)我理解可读性考虑因素,但出于某种原因,我点击它将其写为:

$(document).ready( function () {
    $(".expand").click(function() {
            $("img",this).attr("src", 
                 $("img",this)
                    .attr("src")=="minus.gif" ? "plus.gif" : "minus.gif"
            );
            $(this).parent().next().toggle();
    });
});

...and has the benefit of no extraneous classes.

......并且没有任何无关的课程。

#3


3  

I had to solve this problem recently, but mine involved some nested tables, so I needed a more specific, safer version of javascript. My situation was a little different because I had contents of a td and wanted to toggle the next TR, but the concept remains the same.

我最近不得不解决这个问题,但我的涉及一些嵌套表,所以我需要一个更具体,更安全的javascript版本。我的情况有点不同,因为我有一个td的内容,想要切换下一个TR,但概念保持不变。

$(document).ready(function() {
    $('.expandButton').click(function() {
        $(this).closest('tr').next('tr.expandable').fadeToggle();
    });
});

Closest grabs the nearest TR, in this case the first parent. You could add a CSS class on there if you want to get extremely specific. Then I specify to grab the next TR with a class of expandable, the target for this button. Then I just fadeToggle() it to toggle whether it is displayed or not. Specifying the selectors really helps narrow down what it will handle.

最近的TR抓住最近的TR,在这种情况下是第一个父。如果你想要非常具体,你可以在那里添加一个CSS类。然后我指定使用可扩展类(此按钮的目标)来获取下一个TR。然后我只是fadeToggle()它来切换它是否显示。指定选择器确实有助于缩小它的处理范围。

#4


1  

Try this...

//this will bind the click event
//put this in a $(document).ready or something
$(".expand").click(expand_ClickEvent);

//this is your event handler
function expand_ClickEvent(){
   //get the TR that you want to show/hide
   var TR = $('.expand').parent().next();

   //check its class
   if (TR.hasClass('hide')){
      TR.removeClass('hide'); //remove the hide class
      TR.addClass('show');    //change it to the show class
      TR.show();              //show the TR (you can use any jquery animation)

      //change the image URL
      //select the expand class and the img in it, then change its src attribute
      $('.expand img').attr('src', 'minus.gif');
   } else {
      TR.removeClass('show'); //remove the show class
      TR.addClass('hide');    //change it to the hide class
      TR.hide();              //hide the TR (you can use any jquery animation)

      //change the image URL
     //select the expand class and the img in it, then change its src attribute
      $('.expand img').attr('src', 'plus.gif');
   }
}

Hope this helps.

希望这可以帮助。

#5


-2  

This is how the images are set up in the html

这是在html中设置图像的方式

<tr>

<td colspan="2" align="center"
<input type="image" src="save.gif" id="saveButton" name="saveButton"
    style="visibility: collapse; display: none" 
     onclick="ToggleFunction(false)"/>

<input type="image" src="saveDisabled.jpg" id="saveButtonDisabled" 
      name="saveButton" style="visibility: collapse; display: inline"
      onclick="ToggleFunction(true)"/>
</td>
</tr>

Change the onClick event to your own function that's in JS to toggle between them.

将onClick事件更改为您自己的JS函数,以便在它们之间切换。

In the

ToggleFunction(seeSaveButton){    
    if(seeSaveButton){
        $("#saveButton").attr("disabled", true)
                        .attr("style", "visibility: collapse; display: none;");
        $("#saveButtonDisabled").attr("disabled", true)
                                .attr("style", "display: inline;");
    }    
    else {    
        $("#saveButton").attr("disabled", false)
                        .attr("style", "display: inline;");
        $("#saveButtonDisabled")
                        .attr("disabled", true)
                        .attr("style", "visibility: collapse; display: none;");
    }
}