使用JQuery查找下一个表行

时间:2020-03-06 14:37:23  来源:igfitidea点击:

使用JQuery,如何将click事件绑定到将更改图像src的表单元格(在class =" expand"下)(在被单击的单元格中原始图像为plus.gif,与minus.gif交替出现),以及根据该行是否具有"隐藏"类来隐藏/显示该行的正下方。 (如果它具有" hide"类,则显示它;如果没有" hide"类,则隐藏它)。我可以灵活地更改标记中的ID和类。

谢谢

表格行

<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>

解决方案

我们不需要show和hide标签:

$(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();
    });
});

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

这是在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>

onClick事件更改为JS中我们自己的函数,即可在它们之间进行切换。

在里面

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;");
    }
}

试试这个...

//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');
   }
}

希望这可以帮助。

没有人喜欢三元运算符吗? :)我了解可读性注意事项,但由于某种原因,它让我点击将其写为:

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

...并且没有多余的类的好处。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。