2013-04-22 57 views
0

我有這樣的代碼捆綁,單擊時按鈕,執行代碼隱藏錶行:燒製代碼

<script> 
     $(function hideinstr() { 
      $('tr.parent td').on("click", "input.instr", function() { 
      var idOfParent = $(this).parents('tr').attr('id'); 
      $('tr.child-' + idOfParent).toggle('slow'); 
      }); 
     }); 
</script> 

我想這個代碼在默認情況下,當頁面執行加載,並讓用戶點擊按鈕,如果想透露表格行。當「點擊」內置於此代碼中時,我該如何做到這一點?

回答

0

您的解決方案從不執行功能hideinstr()。另外,如果您的代碼在<head>中執行,請考慮使用$(document).ready。添加額外的括號來執行該功能,或刪除周圍的功能(功能hideinstr)。

<script> 
    $(document).ready(function hideinstr() { 
     $('tr.parent td').on("click", "input.instr", function() { 
     var idOfParent = $(this).parents('tr').attr('id'); 
     $('tr.child-' + idOfParent).toggle('slow'); 
     }); 
    }()); 
</script> 

<script> 
    $(document).ready(
     $('tr.parent td').on("click", "input.instr", function() { 
     var idOfParent = $(this).parents('tr').attr('id'); 
     $('tr.child-' + idOfParent).toggle('slow'); 
     }); 
    ); 
</script> 

,並執行事件腳本直接使用$('tr.parent td').click();

+0

這會如何在頁面加載時觸發? – 2013-04-22 22:12:42

+0

該功能在內置的「點擊」上執行。點擊該按鈕後,該功能起作用。請參閱http://www.faithripple.com/index.htm。 – 2013-04-22 22:15:04

+0

你原來的問題不清楚你想要什麼。要立即執行隱藏指令部分,請使用'$('tr.parent td')。click();'。然而,你應該做的是改變'$('tr.parent td')。css('display','none');'或直接在元素上改變樣式。 – George 2013-04-22 22:18:27