简体   繁体   English

如何在JavaScript onclick函数中运行jQuery代码?

[英]How to run jQuery code in the JavaScript onclick function?

Clicking on the "close" anchor does not close the notification. 单击“关闭” anchor不会关闭通知。

Below is my code: 下面是我的代码:

function show_notification_on_top(message, type) {  

    content =   "<a class='notify-close' onclick='$(\"#notification-box\").fadeOut(\"slow\");' href='#'>close</a>"+ 
                "<p>"+message+"</p>";                   

    $("#notification-box").fadeIn('slow', function() {
            $("#notification-box").delay(60000).fadeOut('slow');
        });

    $("#notification-box").html( content ); 
}

Don't hardcode onclick events on <a> links, use JQuery click unobtrusive subscriber. 不要对<a>链接上的onclick事件进行硬编码,请使用JQuery click不引人注意的订阅者。

function show_notification_on_top(message, type) {  

    content =           
                "<a class='notify-close' href='#'>close</a>"+
                "<p> message </p>";                   

    $("#notification-box").fadeIn('slow', function() {
            $("#notification-box").delay(60000).fadeOut('slow');
        });

    $("#notification-box").html( content ); 

    $('.notify-close').click(function(){
            $('#notification-box').dequeue();
        });
}

Haven't tried the code, but you want something like this... 还没有尝试过代码,但是您想要这样的东西...

function show_notification_on_top(message, type) {                  

    var anc = $('<a>').addClass('notify-close').html('close').click(function() {$('#notification-box').fadeOut();   });

    $("#notification-box").append( anc ).fadeIn('slow', function() {
            $("#notification-box").delay(60000).fadeOut('slow');
    });


}

As it is, this would not work. 实际上,这是行不通的。 Couple of things. 几件事。

First: add the click event into your code, not in the markup you add. 首先:将click事件添加到您的代码中,而不是添加的标记中。 This can simplify your code in the function really. 这样可以真正简化函数中的代码。

Second: your attempt to animate (fadeOut) will fail due to the previous delay and fadeOut in place. 第二:由于先前的延迟和淡出效果,您尝试动画(fadeOut)的操作将失败。 To work this properly, simply dequeue the one you have. 要使其正常工作,只需将您拥有的那个设备出队即可。

function show_notification_on_top(message, type) {
    content = "<a class='notify-close' href='#'>close</a>" + "<p>" + message + "</p>";
    $("#notification-box").fadeIn('slow').delay(60000).fadeOut('slow');
    $("#notification-box").html(content);
}

$(document).on('click', '.notify-close', function() {
    $('#notification-box').dequeue();
});

Note that the .on('click', adds a live event handler, allowing you to remove the event from the markup. 请注意, .on('click',添加了一个实时事件处理程序,允许您从标记中删除事件。

What the code I wrote does: displays message with close you can activate, if not closed manually, waits 60000 miliseconds, then fades out as defined. 我编写的代码的作用是:显示带有关闭状态的消息,您可以激活它,如果没有手动关闭,则等待60000毫秒,然后按照定义淡出。

Here is a working example: http://jsfiddle.net/MarkSchultheiss/X6qDJ/ 这是一个工作示例: http : //jsfiddle.net/MarkSchultheiss/X6qDJ/

EDIT: Note to OP. 编辑:OP的注释。 IF you are fixed on having to include the event as you have it now, you can change your code to: 如果您决定现在必须包含事件,则可以将代码更改为:

content = "<a class='notify-close' onclick='$(\"#notification-box\").dequeue();' href='#'>close</a>" + "<p>" + message + "</p>";

instead of: 代替:

content =   "<a class='notify-close' onclick='$(\"#notification-box\").fadeOut(\"slow\");' href='#'>close</a>" + "<p>"+message+"</p>";

Thanks all of you. 谢谢大家。 With your help, I've written this code. 在您的帮助下,我编写了这段代码。 Works perfectly fine. 工作完美。

function show_notification_on_top(message) {

    content =       "<a class='notify-close' id='notification_anchor' href='#'>close_button_label</a>"+ 
                    "<p>"+message+"</p>";


    $("#notification-box").fadeIn('slow');

    $("#notification-box").html( content );

    $('#notification_anchor').click(function() {
        $("#notification-box").fadeOut("slow");
    });

    window.setTimeout(function() {
        $("#notification-box").fadeOut('slow');
    }, 6000);
}

Add this: 添加:

$(document).ready(function(){
  $(".notify-close").live("click", function(){
    $("#notification-box").fadeOut('slow');
  });
});

and forget about the onclick() event; 忘了onclick()事件;

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

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