简体   繁体   English

jQuery不能识别一个div中的点击事件,但可以识别另一个

[英]Jquery doesn't recognize click event in one div but anothers

So i have this 4 sections, well there are 3 and 1 footer in my html, and jquery applies a css class when they're clicked, the problem is that the footer which btw is a sticky footer using jquery as well, doesn't recognize even the click event. 所以我有这4个部分,我的html中有3个和1个页脚,当单击jquery时,它应用了一个CSS类,问题是btw也是使用jquery的粘性页脚,没有甚至识别点击事件。

<div id="principal">

        <section class="rbox" id="left">
            <em>
                <i class="fa fa-users"></i>
            </em>
        </section>

        <section class="rbox" id="top">
            <em>
                <i class="fa fa-book"></i>
            </em>

        </section>

        <section class="rbox" id="right">
            <em>
                <i class="fa fa-inbox"></i>
            </em>
        </section>


  </div>
        <footer class="rbox" id="bottom">
            <em>
                <i class="fa fa-inbox"></i>
            </em>         
        </footer>   

Here's the js: 这是js:

$(document).on("ready", inicio);
function inicio(){
    var bodyHeight = $("body").height();
    var vwptHeight = $(window).height();

    if(vwptHeight > bodyHeight){
        $("footer#bottom").css("position", "absolute").css("bottom", 0);
    }

    $('.rbox').on("click", despliegue);
    $('footer#bottom').on("click", function(){
        $("footer#bottom").css("background", "red");
    })

};

function despliegue(){
    alert("hola");
    if($(this).hasClass('active')){
        $('.rbox').removeClass('active');
    }
    else{
        $('.rbox').removeClass('active');
        $(this).addClass('active');
    }
};

Another thing is that the alert doesn't show even the function works perfectly but only with the sections the footer not. 另一件事是,即使功能正常运行,警报也不会显示,而仅在页脚区域显示。 What am i doing wrong?? 我究竟做错了什么?? Thanx for your help... 感谢您的帮助...

please try the below way to use your code 请尝试以下方式使用您的代码

$(document).ready(function(){
/////^^^^^^^^^^use ready like this way
inicio();
});

function inicio(){
    var bodyHeight = $("body").height();
    var vwptHeight = $(window).height();

    if(vwptHeight > bodyHeight){
        $("footer#bottom").css("position", "absolute").css("bottom", 0);
    }

    $('.rbox').on("click", function(){despliegue});
    $('footer#bottom').on("click", function(){
        $("footer#bottom").css("background", "red");
    });

}

function despliegue(){
    alert("hola");
    if($(this).hasClass('active')){
        $('.rbox').removeClass('active');
    }
    else{
        $('.rbox').removeClass('active');
        $(this).addClass('active');
    }
} 

Use $(document).ready(inicio); 使用$(document).ready(inicio); instead of $(document).on('ready',inicio); 而不是$(document).on('ready',inicio);

Your Demo and working demo 您的演示工作演示

instead of using tag you can use 而不是使用标签,您可以使用

<div id="footer">Footer Here.</div>

and do operation on that like 并像这样进行操作

#footer {

height: 330px;position: relative; 高度:330像素;位置:相对; } }

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

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