2016-02-03 54 views
-1

我試圖啓動一個:click();函數,但無法做到這一點,我不知道爲什麼,但是當我鍵入:alert();函數在啓動然後我的代碼工作我卡在這種情況。我的代碼無法正常工作();函數在開始

我在互聯網上搜索,但沒有成功。從你的任何幫助是非常可觀的。

HTML:

<div class="edit-options-dated"> 
    <span><?php echo date('H:i:m', (time()-strtotime($note->getCreationDatetime()))); ?></span>   
    <ul class="pull-right edit-task-icons"> 
     <li class="delete-task"><i class="fa fa-trash-o"></i></li> 
     <li class="edit-task"><i class="fa fa-pencil"></i></li> 
     <li class="setting-task"><i class="fa fa-cog"></i></li> 
    </ul> 
    <ul class="settings-task-options"> 
     <h3>note groups</h3>  
     <li><a href="#">new</a></li> 
     <li><a href="#">Affiliate Tasks</a></li> 
     <li><a href="#">next week</a></li> 
    </ul> 
</div> 

JQuery的:

$(window).load(function() { 

// alert('ok'); 

$('li.setting-task').on('click', function() { 

    $(this).parent().parent().find('.settings-task-options').toggle(); 

}); 

$('li.delete-task').click(function() { 

    $('.delete-2').slideDown(); 

}); 

}); 
+1

可以調試這一點?當你附加事件處理程序時,是否找到任何匹配的元素?點擊處理程序是否正在執行,但沒有找到要修改的元素?這種失敗在哪裏/具體如何? – David

+3

如果使用'$(document).ready()'而不是'$(window).load()',它會起作用嗎? – Barmar

+0

刪除'window).load(' – migg

回答

1

你可以試試這個...

$(function(){ // shorthand for document ready - document is safely loaded 
    $(document.body).on('click', 'li.setting-task' ,function(e){ 
     $(this).parent().parent().find('.settings-task-options').toggle(); 
    }); 
    }); 

JQuery的上 - 信息here

document.body basically search the body part of your document for select element and makes event for it 
+0

這個作品非常滿意解決方案,但是你能描述一下,因爲我從來沒有使用這種方法 – qarar

+0

我已經更新了我的答案 – JTC

+0

感謝您的幫助,非常感謝 – qarar

2

,而不是$(window).load();可以使用

$(document).ready(function(){ 
    // code here 
}); 

$(function(){ 
    //code here 
}); 

可能是你需要Event binding on dynamically created elements?

$(document).ready(function(){ 

// alert('ok'); 

$('body').on('click','li.setting-task', function() { 

     $(this).parent().parent().find('.settings-task-options').toggle(); 

    }); 

    $('body').on('click','li.delete-task',function() { 

    $('.delete-2').slideDown(); 

    }); 

}); 
+3

'$(window).on('load',...)'是'$(window).load(...)'。 – Barmar

+0

@Barmar這不是..我只記得我之前使用過它,它不一樣..我真的不記得在哪裏以及如何,但這可能是在另一種情況下比這..但是不要讓OP困惑它..我刪除它。 –

+0

沒有成功@ Mohamed-Yousef – qarar