1

HTML:

<table border="1px">
        <tr>
            <td colspan="2">
                <div class="commentLink">
                    <a onclick="ShowBox.call(this); return false;" href="#">Comment</a>
                </div>
            </td>
        </tr>
        <tr class="commentBox" style="display: none;">
            <td colspan="2">
                <div class="hiddenComment">
                    <textarea class="textComment" rows="2" cols="100"></textarea>
                    <input class="foo" type="checkbox" />
                    <input class="commentBtn" type="button" value="Submit" onclick="addComment.call(this); return false;" />
                    <input class="commentBtn" type="button" value="Cancel" onclick="HideBox.call(this); return false;" />
                </div>
            </td>
        </tr>
    </table>

JS: jquery 1.4.2 を使用

function ShowBox() {
        var that = this;
        $(function () {
            $(".commentBox").show();
            //$(that).closest('tr').siblings().show();
        });
    }
    function HideBox() {
        var that = this;
        $(function () {
            $(that).siblings(".foo").attr("checked", false);
            $(that).siblings(".textComment").empty();
            $(".commentBox").hide();
        });
    }  

tr を表示/非表示にする 2 つの関数があります。私が今持っているコードは機能しますが、他の要素も閉じます。これを行うエレガントな方法は何ですか?

4

2 に答える 2

1

これはそれを行う必要があります:

function ShowBox() {
    $(this).closest ('tr').next ('tr.commentBox').show ();
}

function HideBox() {
    var jThis   = $(this);

    jThis.siblings(".foo").attr("checked", false);
    jThis.siblings(".textComment").empty ();
    jThis.closest ('tr.commentBox').hide ();
} 


ところで、「エレガント」の場合onclickは、HTML から属性を削除してください。

次に、次のようにクリック機能を追加します。

$('td div.commentLink > a').bind ('click', ShowBox, false);
$('td div.hiddenComment > a.commentBtn[value=Submit]').bind ('click', addComment, false);
$('td div.hiddenComment > a.commentBtn[value=Cancel]').bind ('click', HideBox, false);

の中に$(document).ready ()

于 2011-07-16T22:23:17.757 に答える
0

closestセレクターはバージョン 1.3 で追加されたと思いますので、それを使用してください。

于 2011-07-16T22:15:47.743 に答える