Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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
将jQuery代码移动到函数_Jquery - Fatal编程技术网

将jQuery代码移动到函数

将jQuery代码移动到函数,jquery,Jquery,我有以下jQuery代码: $('.show-additional-link').click(function(){ $(this).parent().next().slideDown(); $(this).hide(); return false; }); HTML: 挑选 任何 英语 我想将jQuery代码的内容(在“click”函数中)移动到一个单独的函数中,因为我需要在页面加载时再次调用它(在表单提交后,这样DIV会自动再次显示) 我遇到了问题-有人能帮忙吗

我有以下jQuery代码:

$('.show-additional-link').click(function(){
    $(this).parent().next().slideDown();
    $(this).hide();
    return false;
});
HTML:


挑选
任何
英语
我想将jQuery代码的内容(在“click”函数中)移动到一个单独的函数中,因为我需要在页面加载时再次调用它(在表单提交后,这样DIV会自动再次显示)

我遇到了问题-有人能帮忙吗?

您尝试过:

$('.show-additional-link').each(function() {makeClickable($(this));}));


function makeClickable(el) { 
    el.click(function(){
        el.parent().next().slideDown();
        el.hide();
        return false;
    });
}
<script type="text/javascript">
  function showAdditional() {
    $('.show-additional-link').parent().next().slideDown();
    $('.show-additional-link').hide();
  }
</script>

函数showAdditional(){
$('.show additional link').parent().next().slideDown();
$('.show additional link').hide();
}
并将show additional链接href更改为“javascript:showAdditional()”?

尝试:

$('.show-additional-link').click(function() {return showAdditional(this);});

function showAdditional(e) {
    $(e).parent().next().slideDown();
    $(e).hide();
    return false;
}

在页面加载类似于
showAdditional($(“#linkyouwantclicked”)
的内容时,您可以手动触发事件。这应该为处理程序保留正确的上下文,而不会带来任何额外的麻烦

jQuery(function ($) {
    // when the document has loaded
    $('.show-additional-link')
        .click(function () {    // set up the handler
            // your code here
        })
        .click()                // trigger the handler
    ;
});

@Pekka可能与
这个
有关。我已经尽力了,但我还在努力。我不确定要传递给函数的值是什么。但是如果我在页面加载时调用makeClickable(el),它将不起作用,因为它正在寻找一个“单击”操作。确定正常的单击对此很有效,但在页面加载时不起作用。仔细想想,我认为A标签需要一个ID——这样我们就可以在页面加载时直接引用那个特定的A标签。如何修改代码以引用ID标记呢?您只需为每个标记分配一个唯一的ID,然后设置onload并将其传递给idCheers mate,所有内容都已排序。将进行进一步的测试,如果一切正常,将接受你的回答:)嘿,我不知道这个把戏-太棒了!它可以工作,但有一个问题-我只希望在选中DIV中的复选框(即,如果$\u GET数组中存在checkbox变量)时发生此触发事件。我们可以手动运行这个触发器吗?
jQuery(function ($) {
    // when the document has loaded
    $('.show-additional-link')
        .click(function () {    // set up the handler
            // your code here
        })
        .click()                // trigger the handler
    ;
});