简体   繁体   English

jQuery一键二功能

[英]Jquery one click two function

I want to execute 2 functions on click. 我想点击执行2个功能。 Separately, both work great. 另外,两者都很好。 However, I have a problem when I execute functionA and after functionB. 但是,我在执行functionA时和在functionB之后都遇到问题。

The problem come frome the functionB. 问题来自功能B。 I think it's because of the $(this). 我认为是因为$(this)。 It don't get the correct id because of all the precedent var executed on the functionA. 由于在functionA上执行了所有先前的var,因此无法获得正确的ID。 THe id call by $(this) must be #prev-ajax,#next-ajax . $(this)进行的ID调用必须是#prev-ajax,#next-ajax。

Here the code : 这里的代码:

function A(){
    var link = $(this); 
    ajaxify(link.attr('href'));
    window.location.hash = link.attr("href");
    $('.link').removeClass('current');
}
function B(){
    e.preventDefault();
    var href = $(this).attr('href');
    var link = $(".link[href*= '" + href + "']:not(#prev-ajax, #next-ajax)");
    var $parent = link.parents('.element');
    var prev = $parent.prev().find('.link').attr('href');
    var next = $parent.next().find('.link').attr('href');
    $("#prev-ajax").attr( 'href', prev );
    $("#next-ajax").attr( 'href', next ); 
}
$("#prev-ajax,#next-ajax").click(A).click(B);

Sorry for English, I'm french 对不起英语,我是法语

Loïc 洛伊奇

Try this once and see if you are still having issue using both of the functions together: 尝试一次,看看同时使用两个功能是否仍有问题:

$("#prev-ajax,#next-ajax").click(function (e) {
    e.preventDefault();
    var functionAlink = $(this);
    var functionBlink = $(this);

    // Function A code
    ajaxify(functionAlink.attr('href'));
    window.location.hash = functionAlink.attr("href");
    $('.link').removeClass('current');

    // Function B code
    var href = functionBlink.attr('href');
    var link = $(".link[href*= '" + href + "']:not(#prev-ajax, #next-ajax)");
    var $parent = link.parents('.element');
    var prev = $parent.prev().find('.link').attr('href');
    var next = $parent.next().find('.link').attr('href');
    $("#prev-ajax").attr('href', prev);
    $("#next-ajax").attr('href', next);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM