2017-06-01 8 views
0

私はこのjqueryを持っています。ボタンをクリックするとロードされますが、ページがロードされたときに関数をロードします。コンバートClick to page load jquery

 $("[src*=plus]").live("click", function() { 
      $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>") 
      $(this).attr("src", "images/minus.png"); 
     }); 

$("[src*=minus]").live("click", function() { 
     $(this).attr("src", "images/plus.png"); 
     $(this).closest("tr").next().remove(); 
    }); 

私はこれを使って試してみたが、それは仕事をdoesntの:

$(document).ready(function() { 
     $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>") 
     $(this).attr("src", "images/minus.png"); 
    }); 

    $(document).ready(function() { 
     $(this).attr("src", "images/plus.png"); 
     $(this).closest("tr").next().remove(); 
    }); 
+1

を試してみてください。これはjQueryのどのバージョンですか? .liveは推奨されていませんか? – ThisGuyHasTwoThumbs

+0

不明です。これは私がコードを見つけた場所です:https://www.aspsnippets.com/Articles/ASPNet-Nested-GridViews-GridView-inside-GridView-with-Expand-and-Collapse-feature.aspx –

+0

ライブは実際には削除されましたバージョン1.9以降](http://api.jquery.com/category/removed/#post-409)。 –

答えて

1

は、あなたが唯一の$(ドキュメント).ready()を使用し、内部のすべてを置くこの

$(document).ready(function() { 
    $("[src*=plus]").closest("tr").after("<tr><td></td><td colspan = '999'>" + 
    $("[src*=plus]").next().html() + "</td></tr>") 
    $("[src*=plus]").attr("src", "images/minus.png"); 
    $("[src*=plus]").attr("src", "images/plus.png"); 
    $("[src*=plus]").closest("tr").next().remove(); 
}); 
+0

ありがとう!これは動作します! –

+2

@ThisGuyHasTwoThumbsその通りです。入力していただきありがとうございます –

+0

@FurquanKhan素晴らしいコメントを削除します:) – ThisGuyHasTwoThumbs