Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以在其他功能中访问克隆?_Javascript_Jquery_Variables_Clone - Fatal编程技术网

Javascript 是否可以在其他功能中访问克隆?

Javascript 是否可以在其他功能中访问克隆?,javascript,jquery,variables,clone,Javascript,Jquery,Variables,Clone,我有一个名为taskButtonClick()的函数: 将变量形式设置为克隆 将变量formParent设置为最外层的div 删除父div及其元素 并将这些删除的元素替换为表单 function taskButtonClick() { $(".task-btn").click(function(){ // Setting form to the clone I want to acces in .on() var form = $(this).closest('#form-to-

我有一个名为taskButtonClick()的函数:

  • 将变量形式设置为克隆
  • 将变量formParent设置为最外层的div
  • 删除父div及其元素
  • 并将这些删除的元素替换为表单

    function taskButtonClick() {
    $(".task-btn").click(function(){
        // Setting form to the clone I want to acces in .on()
        var form = $(this).closest('#form-to-date').clone(true);
        var formParent = $(this).closest('#date-container');
        $(this).closest('#form-to-date').remove();
            $(formParent).html('\
                <form>\
                    <fieldset>\
                    <legend>Task<button class="btn task-btn" id="exit- button"><img src="http://localhost/ia/assets/img/remove.png"></button></legend>\
                     <input type="text" class="span12 input-xlarge"  id="input01">\
                    <div class="control-group">\
                        <div class="controls">\
                        </div>\
                    </div>\
                    <div class="form-actions">\
                        <button class="btn btn-primary span12"  type="submit">Save changes</button>\
                    </div>\
                    </fieldset>\
                </form>\
                ');
    });
    }
    
    我对jquery不是很熟悉,所以如果我能更好地澄清,请告诉我

    Edit: Here's where I'm calling the funcitons:
    
    $('document').ready(function(){
        setAjax();
        initialize();
        setDates();
        setButton(); 
        setActiveNav(); 
        taskButtonClick();
        removeButtonClicked();
    });
    

    尝试声明一个全局变量

    $(document).ready(function(){
           //Notice variable is outside of the event function. It can be accessed by other functions and events.
           var formParent = null;
    
           $(".task-btn").click(function(){
        // Setting form to the clone I want to acces in .on()
        var form = $(this).closest('#form-to-date').clone(true);
        formParent = $(this).closest('#date-container');
        $(this).closest('#form-to-date').remove();
            $(formParent).html('\
                <form>\
                    <fieldset>\
                    <legend>Task<button class="btn task-btn" id="exit- button"><img src="http://localhost/ia/assets/img/remove.png"></button></legend>\
                     <input type="text" class="span12 input-xlarge"  id="input01">\
                    <div class="control-group">\
                        <div class="controls">\
                        </div>\
                    </div>\
                    <div class="form-actions">\
                        <button class="btn btn-primary span12"  type="submit">Save changes</button>\
                    </div>\
                    </fieldset>\
                </form>\
                ');
         });
    });
    
    $(文档).ready(函数(){
    //注意变量不在事件函数的范围内。其他函数和事件可以访问它。
    var formParent=null;
    $(“.task btn”)。单击(函数(){
    //将表单设置为要访问的克隆。on()
    var form=$(this).clone(true);
    formParent=$(this).closest(“#日期容器”);
    $(this).最近(“#表单到日期”).remove();
    $(formParent).html($)\
    \
    \
    任务\
    \
    \
    \
    \
    \
    \
    保存更改\
    \
    \
    \
    ');
    });
    });
    
    如果要使用克隆变量,请使用全局变量声明。

    为什么在函数中有.task btn click事件?@MikeGB可能是因为我做错了什么。我编写jquery的方法是创建
    $(document).ready(function(){})
    ,并在这些括号中调用所有函数。您的方法可以工作,但不需要。事件将在单击时执行,您不必调用函数来触发事件。但是它应该仍然在$(document).ready(function(){})中。我最初没有尝试这种方法的唯一原因是因为我听说使用global不是一个好主意。这是例外吗?唯一的区别是数据仍将可用于应用程序。处理完成后,局部变量(函数内部)将被清除。全局变量不是“坏的”,它们只是停留。不过,这是您希望使用的,因为您希望在应用程序中全局访问变量。
    $(document).ready(function(){
           //Notice variable is outside of the event function. It can be accessed by other functions and events.
           var formParent = null;
    
           $(".task-btn").click(function(){
        // Setting form to the clone I want to acces in .on()
        var form = $(this).closest('#form-to-date').clone(true);
        formParent = $(this).closest('#date-container');
        $(this).closest('#form-to-date').remove();
            $(formParent).html('\
                <form>\
                    <fieldset>\
                    <legend>Task<button class="btn task-btn" id="exit- button"><img src="http://localhost/ia/assets/img/remove.png"></button></legend>\
                     <input type="text" class="span12 input-xlarge"  id="input01">\
                    <div class="control-group">\
                        <div class="controls">\
                        </div>\
                    </div>\
                    <div class="form-actions">\
                        <button class="btn btn-primary span12"  type="submit">Save changes</button>\
                    </div>\
                    </fieldset>\
                </form>\
                ');
         });
    });