简体   繁体   English

外部 div 有点击 function 但内部 div 不能

[英]Outer div has click function but inner div cannot

I don't know how to effectively title the problem but here's the situation.我不知道如何有效地为问题命名,但情况就是这样。

I have an overlay that is a login window that pops up on a page that a non logged in user cannot access.我有一个覆盖,它是一个登录 window,它会在非登录用户无法访问的页面上弹出。 Since the user cannot access that page I can't have the normal functioning where if you click outside the pop up it just removes itself from the page.由于用户无法访问该页面,因此我无法正常运行,如果您在弹出窗口外部单击,它只会将自身从页面中删除。 So what I have is a re-direct to the sign up page.所以我所拥有的是重定向到注册页面。 However since this click function is on the outer div, on the login form inside this div when the user goes to enter the credentials it re-directs.但是,由于此单击 function 位于外部 div 上,因此当用户输入重定向的凭据时,在此 div 内的登录表单上。

So I need to not allow it on the form.所以我需要不允许它出现在表格上。

So I did this:所以我这样做了:

$('#logform').click(false);

which works, but now the button to login wont work?哪个有效,但现在登录按钮不起作用?

Example:例子:

    <div id="maindiv>
    <form id="logform">
    <input type="text" name="exampleinput">
    <button type="submit" id="loginbutton">
    </form>
    </div>
<script>
    $(document).ready(function(){
        $('.maindiv').show();
        $('.maindiv').click(function(){
            window.location.href='/users/account-info';
        });
    
        $('#logform').click(false);
    
    });


</script>

So how can I disable the href click function for the form but also allow the login button to function correctly?那么如何才能正确禁用表单的href单击function,但也允许登录按钮正确地指向function?

All you need to do is determine what the element was that the event was triggered on, which can be determined by looking at the event object's .target property.您需要做的就是确定触发事件的元素是什么,这可以通过查看event对象的.target属性来确定。 The event object is automatically passed to all DOM event handlers that are registered using a JQuery binding or the vanilla .addEventListener() method. event object 会自动传递给使用 JQuery 绑定或 vanilla .addEventListener .addEventListener()方法注册的所有 DOM 事件处理程序。

 #maindiv { background-color:grey; width:100vw; height:100vh; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="maindiv"> <form id="logform"> <input type="text" name="exampleinput"> <button type="submit" id="loginbutton">Submit</button> </form> </div> <script> $(document).ready(function(){ // Set the event handler up so that it recieves the event reference $('#maindiv').on("click", function(event){ // Determine where the event was triggered // If the click was on the div... if(event.target === this){ location.href='http://www.example.com'; // Redirect } }); }); </script>

Can you change your code?你能改变你的代码吗? Or is it auto-generated?还是自动生成的? If you can change your code I highly recommend you place the form into another div outside of your main div, like so:如果您可以更改代码,我强烈建议您将表单放入主 div 之外的另一个 div 中,如下所示:

    <div id="maindiv">
    </div>
    <div id="formdiv">
        <form id="logform">
            <input type="text" name="exampleinput">
            <button type="submit" id="loginbutton">
        </form>
    </div>
<script>
    $(document).ready(function(){
        $('.maindiv').show();
        $('.maindiv').click(function(){
            window.location.href='/users/account-info';
        });
    
        $('#logform').click(false);
    
    });


</script>

This would allow you to, with a bit of CSS click the form without clicking the surrounding div.这将允许您使用一点 CSS 单击表单而不单击周围的 div。 Assuming you want to freeze your screen so that the user can't scroll, use position: absolute;假设您想冻结屏幕以使用户无法滚动,请使用position: absolute; in your CSS file.在您的 CSS 文件中。 If you want to let your user scroll but lack the ability to interact with anything, use position: fixed;如果您想让您的用户滚动但缺乏与任何东西交互的能力,请使用position: fixed; . . This allows your form to be on top of or under other elements.这允许您的表单位于其他元素之上或之下。 Then, set z-index to anything greater than the z-index of your background div, or, if you haven't set it yet, write z-index: 2;然后,将z-index设置为大于背景 div 的z-index的任何值,或者,如果尚未设置,请编写z-index: 2; . . The higher z-index when two elements are in the same place is the one that ends up over the other element.当两个元素在同一个位置时,较高的z-index是最终超过另一个元素的那个。 This means that your form is on top of(or in front of, they mean the same anyway) your background div, meaning when you click it, you don't actually click the div around it.这意味着您的表单位于您的背景 div 之上(或前面,它们的含义相同),这意味着当您单击它时,您实际上并没有单击它周围的 div。 This allows you to get rid of $('#logform').click(false);这使您可以摆脱$('#logform').click(false);

Hope this answer helps you!希望这个答案对你有帮助!

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

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