在函数

时间:2017-01-20 22:15:34

标签: jquery function

我有一个为我编写jQuery函数的函数,但是当运行jQuery(一个click事件)时,代码似乎从来没有document.ready编辑它,因为它不存在“在一开始的时候”。如何在函数创建代码后“准备好”代码?

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

2 个答案:

答案 0 :(得分: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);
    });

});

答案 1 :(得分:0)

我想在你的" readyclick"功能完成后,你可以简单地做一个

$.ready();

这应该负责执行ready回调中的jquery代码。