Можно ли вызвать функцию jquery одним щелчком мыши? - PullRequest
0 голосов
/ 09 августа 2009

Я пытаюсь сделать что-то похожее на окно оповещения, которое мы получаем здесь, на SO, когда мы получаем новые комментарии и ответы, оранжевое поле, которое прилипает к верхней части экрана, у меня что-то похожее, но я пытаюсь получить X закрыть ссылку на него, чтобы закрыть его при нажатии

Вот что у меня есть, но оно дает мне ошибки, говоря, что close_error_click () не определен?

<script type="text/javascript" >
function close_error_click(){
    $("#notify-container").fadeOut("slow", function () {
    $("#notify-container").remove();
}
</script>

<div id="notify-container">
some other code here
<a onclick="close_error_click" title="dismiss this notification">×</a>
</div>

Ответы [ 6 ]

4 голосов
/ 09 августа 2009

Вместо использования атрибута onlick вы можете добавить следующую строку после своей функции:

$("#notify-container a").click(close_error_click);

Этот метод, известный как Ненавязчивый JavaScript , поддерживает чистоту разметки от Javascript. Вот полный пример:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        $(function()
        {
            $("#notify-container a.close").click(function()
            {
                $("#notify-container").fadeOut("slow", function()
                {
                    $(this).remove();
                });
            });
        });
        </script>
    </head>
    <body>
        <div id="notify-container">
            some other code here
            <a href="#" class="close" title="dismiss this notification">×</a>
        </div>
    </body>
</html>
1 голос
/ 09 августа 2009

Попробуйте

<a onclick="close_error_click();" title="dismiss this notification">×</a>
1 голос
/ 09 августа 2009

Да, просто сделайте:

close_error_click(); в вашем клике.

Вы пропустили скобки на конце.

0 голосов
/ 09 августа 2009
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
    <div id="notify-container">
        some other code here <a id="CloseLink" href="#" title="dismiss this notification">x</a>
    </div>
</body>
<script type="text/javascript">
    $(function()
    {
        $('div#notify-container a#CloseLink').click(function(e)
        {
            $('div#notify-container').fadeOut('slow', function()
            {
                $(this).remove();
            });
            e.preventDefault();
        });
    });
</script>
</html>
0 голосов
/ 09 августа 2009

вам нужно добавить () к названию функции ×

или в jquery

$ (документ) .ready (function () {

$ ( "# id_of_the_a_element") нажмите кнопку (close_error_click).

}); * +1011 *

function close_error_click () {
$ ("# notify-container"). fadeOut ("медленно", function () {$ ("# notify-container"). remove ();}

0 голосов
/ 09 августа 2009

Кроме того, вам может не понадобиться код в разметке:

<script type="text/javascript" >
function close_error_click(){
    $("#notify-container").fadeOut("slow", function () {
    $("#notify-container").remove();
    return false;
});
}
$(document).ready(function(){
$('#notify-container .closewindow").click(close_error_click);
});
</script>

<div id="notify-container">
some other code here
<a href="#" title="dismiss this notification" class="closewindow">×</a>
</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...