2017-01-20 18 views
0

我有一個函數爲我寫了一個jQuery函數,但是當它運行該jQuery(一個單擊事件)時,代碼似乎從來沒有document.ready編輯它,因爲它在開始時並不存在「。如何在代碼被函數創建之後「準備好」代碼?在函數中創建後準備好jQuery

function readyclick(array){ 
    $("script").append("$(document).ready(function({ 
     $('#"+array+"').on('click', function(){$('#"+array+"').hide(); 
    } 
} 

回答

0

您必須按以下方式編寫它,以確保您的代碼在文檔準備就緒時正確執行。

$(function(){ 
//what evenr written inside this code will get executed on document ready 

    // it seems you need to add some event. Usually events are added to HTML 
    // controls and can be easily identified with help of ID or Name or Class 
    // ++array++ does't seems name or id of any element. so check which html you 
    //want to bind your event with. 
    // if it is a class write ".CLASS_NAME" if it is ID write "#Element ID" 
    $("Your_element_selector").click(function(){ 
    /what ever you write inside this function will executed on click event of this element 
    $(this).hide(); // you can access element using $(this); 
    }); 

}); 
+0

你說得對!我忘記了「#」。但它仍然不會執行,當我點擊它... – CamdyCorn

0

我想你「readyclick」函數執行完畢後,你可以簡單地做一個

$.ready(); 

這應該照顧執行jQuery代碼是在準備回調。

+0

所以我正在嘗試它,也許我只是把它錯了?這會去哪裏? – CamdyCorn