FadeOut用于单击的同一链接

时间:2011-10-07 14:51:46

标签: javascript jquery

我希望点击每个链接,点击ok myalert()中的按钮fadeOut()相同的链接fadeOut()。如果点击取消,则不是myalert()。如何使用函数function myalert() { var result = true; //var hide = $('.alert').fadeOut(100); //var css = $('#appriseOverlay').css('display','none'); var $alertDiv = $('<div class="alert">Do you want to delete this item?<button class="ok">ok</button><button class="cancel">cancel</button></div>'); $('body').append($alertDiv); $('.ok').click(function () { $('.alert').fadeOut(100); $('#appriseOverlay').css('display', 'none'); callback(true); }); $('.cancel').click(function () { $('.alert').fadeOut(100); $('#appriseOverlay').css('display', 'none'); callback(false); }); $alertDiv.fadeIn(100); $('#appriseOverlay').css('display', 'block'); return result; }; $('.iu').click(myalert) function callback(result) { // if (result) { $(this).fadeOut('slow');//I mean, this part from code } else { alert('no') } } 修复它?

示例: http://jsfiddle.net/MaGyp/11/

{{1}}

2 个答案:

答案 0 :(得分:0)

更改您的回调实现,以传递所点击的链接的值(jsFiddle)。

function myalert() {
    var result = true;
    //var hide = $('.alert').fadeOut(100);
    //var css = $('#appriseOverlay').css('display','none');
    var $alertDiv = $('<div class="alert">Do you want to delete this item?<button class="ok">ok</button><button class="cancel">cancel</button></div>');

    var link = this; //save the clicks link
    $('body').append($alertDiv);
    $('.ok').click(function () {
        $('.alert').fadeOut(100);
        $('#appriseOverlay').css('display', 'none');
        callback(true, link); //pass along the clicked link
    });
    $('.cancel').click(function () {
        $('.alert').fadeOut(100);
        $('#appriseOverlay').css('display', 'none');
        callback(false, link);
    });
    $alertDiv.fadeIn(100);
    $('#appriseOverlay').css('display', 'block');
    return result;
};

$('.iu').click(myalert)

function callback(result, link) {
    if (result) {
        $(link).fadeOut('slow');//fade out the clicked link
    } else {
        alert('no')
    }
}

答案 1 :(得分:0)

这是一个知道你在callback函数中点击了什么元素的问题:

// pass e (the event object to "myalert")
function myalert(e) {
    var result = true;
        //var hide = $('.alert').fadeOut(100);
        //var css = $('#appriseOverlay').css('display','none');
        var $alertDiv = $('<div class="alert">Do you want to delete this item?<button class="ok">ok</button><button class="cancel">cancel</button></div>');
    $('body').append($alertDiv);
    $('.ok').click(function () {
        $('.alert').fadeOut(100);
        $('#appriseOverlay').css('display','none');

        // pass the clicked element to the callback function
        callback(true, e.target);

    });
    $('.cancel').click(function () {
        $('.alert').fadeOut(100);
        $('#appriseOverlay').css('display','none');

        // pass the clicked element to the callback function
        callback(false, e.target);

    });
    $alertDiv.fadeIn(100);
    $('#appriseOverlay').css('display','block');
return result;
};

$('.iu').click(myalert) // the event object will be passed to myalert

// takes the result, and the clicked element
function callback(result, el) {
    //
    if(result){
       $(el).fadeOut('slow'); 
    }else{
       alert('no')
    }
}

http://jsfiddle.net/swatkins/fLSeD/