使用Jquery / Javascript Notification Popup单击链接时无法显示值

时间:2018-03-28 13:57:21

标签: javascript jquery html

我从http://www.encodedna.com/jquery/create-a-facebook-like-notifications-using-jquery-css.htm下载了此弹出式通知。

我想用它来显示价值,例如。 (11)在单击链接时的模态弹出窗口中。

当我单击页面正文上的链接时,它工作正常并显示值11.问题是何时 我从弹出通知菜单中单击链接,它不显示任何值。似乎通知Popup的Document()函数是冲突的 使用Document()函数来帮助显示数据。

(注意:如果您首先单击正文上的链接,然后单击通知弹出窗口内的链接,则会显示值)

我只想通过点击通知弹出菜单中的链接来显示数据。谢谢

以下是菜单通知弹出窗口的Javascript

<script>
    $(document).ready(function () {

        // ANIMATEDLY DISPLAY THE NOTIFICATION COUNTER.
        $('#noti_Counter')
            .css({ opacity: 0 })
            .text('7')              // ADD DYNAMIC VALUE (YOU CAN EXTRACT DATA FROM DATABASE OR XML).
            .css({ top: '-10px' })
            .animate({ top: '-2px', opacity: 1 }, 500);

        $('#noti_Button').click(function () {

            // TOGGLE (SHOW OR HIDE) NOTIFICATION WINDOW.
            $('#notifications').fadeToggle('fast', 'linear', function () {
                if ($('#notifications').is(':hidden')) {
                    $('#noti_Button').css('background-color', '#2E467C');
                }
                else $('#noti_Button').css('background-color', '#FFF');        // CHANGE BACKGROUND COLOR OF THE BUTTON.
            });

            $('#noti_Counter').fadeOut('slow');                 // HIDE THE COUNTER.

            return false;
        });

        // HIDE NOTIFICATIONS WHEN CLICKED ANYWHERE ON THE PAGE.
        $(document).click(function () {
            $('#notifications').hide();

            // CHECK IF NOTIFICATION COUNTER IS HIDDEN.
            if ($('#noti_Counter').is(':hidden')) {
                // CHANGE BACKGROUND COLOR OF THE BUTTON.
                $('#noti_Button').css('background-color', '#2E467C');
            }
        });

        $('#notifications').click(function () {
            return false;       // DO NOTHING WHEN CONTAINER IS CLICKED.
        });
    });
</script>

这是在点击时以模态显示数据的javascript

<script>

$(document).on("click", ".opendata", function () {

     var myuserid1a = $(this).data('userid1a');

     $(".modal-body_chat #b_userid1a").val( myuserid1a );
});


</script>

这是整个代码

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">


<style>
    ul {
        display:block;
        background:#45619D;
        list-style:none;
        margin:0;
        padding:12px 10px;
        height:21px;
    }
    ul li {
        float:left;
        font:13px helvetica;
        font-weight:bold;
        margin:3px 0;
    }
    ul li a {
        color:#black;
        text-decoration:none;
        padding:6px 15px;
        cursor:pointer;
    }


    /* A CIRCLE LIKE BUTTON IN THE TOP MENU. */
    #noti_Button {
        width:202px;
        height:22px;
        line-height:22px;
        border-radius:50%;
        -moz-border-radius:50%; 
        -webkit-border-radius:50%;
        background:#FFF;
        margin:-3px 10px 0 10px;
        cursor:pointer;
    }

    /* THE POPULAR RED NOTIFICATIONS COUNTER. */
    #noti_Counter {
        display:block;
        position:absolute;
        background:#E1141E;
        color:#FFF;
        font-size:12px;
        font-weight:normal;
        padding:1px 3px;
        margin:-8px 0 0 25px;
        border-radius:2px;
        -moz-border-radius:2px; 
        -webkit-border-radius:2px;
        z-index:1;
    }

    /* THE NOTIFICAIONS WINDOW. THIS REMAINS HIDDEN WHEN THE PAGE LOADS. */
    #notifications {
        display:none;
        width:430px;
        position:absolute;
        top:30px;
        left:0;
        background:#FFF;
        border:solid 1px rgba(100, 100, 100, .20);
        -webkit-box-shadow:0 3px 8px rgba(0, 0, 0, .20);
        z-index: 0;
    }
    /* AN ARROW LIKE STRUCTURE JUST OVER THE NOTIFICATIONS WINDOW */
    #notifications:before {         
        content: '';
        display:block;
        width:0;
        height:0;
        color:transparent;
        border:10px solid #CCC;
        border-color:transparent transparent #FFF;
        margin-top:-20px;
        margin-left:10px;
    }

</style>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).on("click", ".opendata", function () {

     var myuserid1a = $(this).data('userid1a');

     $(".modal-body_chat #b_userid1a").val( myuserid1a );
});


</script>


<br><br>
    <div>
        <ul>
            <li id="noti_Container">
                <div id="noti_Counter"></div>   <!--SHOW NOTIFICATIONS COUNT.-->

                <!--A CIRCLE LIKE BUTTON TO DISPLAY NOTIFICATION DROPDOWN.-->
                <div style="color:white:" id="noti_Button">Click from notification menu</div>    

                <!--THE NOTIFICAIONS DROPDOWN BOX.-->
                <div id="notifications">
                    <h3>Notifications</h3>

<br><br>

<a href="#"   data-userid1a="11"  class="myBtn_chat opendata">click to Display Value Now</a>


                    <div style="height:300px;"></div>
                    <div class="seeAll"><a href="#">See All</a></div>
                </div>
            </li>

        </ul>
    </div>



<script>
    $(document).ready(function () {

        // ANIMATEDLY DISPLAY THE NOTIFICATION COUNTER.
        $('#noti_Counter')
            .css({ opacity: 0 })
            .text('7')              // ADD DYNAMIC VALUE (YOU CAN EXTRACT DATA FROM DATABASE OR XML).
            .css({ top: '-10px' })
            .animate({ top: '-2px', opacity: 1 }, 500);

        $('#noti_Button').click(function () {

            // TOGGLE (SHOW OR HIDE) NOTIFICATION WINDOW.
            $('#notifications').fadeToggle('fast', 'linear', function () {
                if ($('#notifications').is(':hidden')) {
                    $('#noti_Button').css('background-color', '#2E467C');
                }
                else $('#noti_Button').css('background-color', '#FFF');        // CHANGE BACKGROUND COLOR OF THE BUTTON.
            });

            $('#noti_Counter').fadeOut('slow');                 // HIDE THE COUNTER.

            return false;
        });

        // HIDE NOTIFICATIONS WHEN CLICKED ANYWHERE ON THE PAGE.
        $(document).click(function () {
            $('#notifications').hide();

            // CHECK IF NOTIFICATION COUNTER IS HIDDEN.
            if ($('#noti_Counter').is(':hidden')) {
                // CHANGE BACKGROUND COLOR OF THE BUTTON.
                $('#noti_Button').css('background-color', '#2E467C');
            }
        });

        $('#notifications').click(function () {
            return false;       // DO NOTHING WHEN CONTAINER IS CLICKED.
        });
    });
</script>



<br><br>

<a href="#opendata"   data-userid1a="11"  class="myBtn_chat opendata">click to Display from the body</a>



<!-- The Modal  chat menu starts-->



<style>
body {font-family: Arial, Helvetica, sans-serif;}

/* The Modal (background) */
.modal_chat {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content_chat {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0} 
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close_chat {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close_chat:hover,
.close_chat:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header_chat {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
height:10%;
}
.modal-body_chat {
    padding: 2px 16px;
    max-height: calc(100% - 25%);
    overflow: auto;
}
//.modal-body {padding: 2px 16px;}

.modal-footer_chat {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
postion:fixed;
height:15%;
}
</style>




<div id="myModal_chat" class="modal_chat">

  <!-- Modal content -->
  <div  class="modal-content_chat">
    <div class="modal-header_chat">
      <span class="close_chat">&times;</span>

    </div>
    <div class="modal-body_chat">

<input type="text"  name='b_userid1a' id='b_userid1a' >

    </div>
    <div style="" class="modal-footer_chat">
    info 
    </div>
  </div>

</div>



<script>
var modal_chat = document.getElementById('myModal_chat');
var span = document.getElementsByClassName("close_chat")[0];
var btns = document.getElementsByClassName("myBtn_chat");

for (var i = 0; i < btns.length; i++)
  btns[i].onclick = function() {
    modal_chat.style.display = "block";
  }
span.onclick = function() {
    modal_chat.style.display = "none";
}

window.onclick = function(event) {
    if (event.target == modal_chat) {
        modal_chat.style.display = "none";
    }
}
</script>








<!-- The Modal  chat menu ends-->

</body>
</html>

1 个答案:

答案 0 :(得分:0)

问题在于下面的代码

$('#notifications').click(function () {
            return false;       // DO NOTHING WHEN CONTAINER IS CLICKED.
        });

根据脚本的作者,上面的函数说单击容器时什么都不做。为此,单击链接时,它不会显示任何数据。因此删除上面的脚本解决了我的问题。感谢