简体   繁体   English

在页面加载时隐藏div类

[英]Hide div class on page load

Hello I have a sign up form which is visible and I am trying to hide it when the page loads. 您好我有一个可见的注册表单,我试图在页面加载时隐藏它。 So what I did was to put the whole form in a div class named "signup" and then hide it with a script code. 所以我做的是将整个表单放在名为“signup”的div类中,然后用脚本代码隐藏它。

UPDATE: html 更新:HTML

<script>
  document.write('<div class="js">');
  </script>

<span class="button">Sign In</span>
<div id="signup">
<div class="modal-bg">
<div id="modal">
    <span>Sign In<a href="#close" id="close">&#215;</a></span>
    <form>
        <input id="username" name="username" type="textbox" placeholder="Username" required>
        <input id="password" name="password" type="password" placeholder="Password" required>
        <a id="forgot-link" href="#">Forgot password?</a>
        <button name="submit" id="submit" type="submit">Sign in</button>
    </form>
</div>
</div>
</div>
<script>
  document.write('</div>');
  </script>

My JS code (without hide function): 我的JS代码(没有隐藏功能):

$('.button').click(function(){
          $('#modal').css('display','block');
          $('.modal-bg').fadeIn();
    });

        $('#close,.modal-bg').click(function(){
              $('.modal-bg').fadeOut();     
              $('#modal').fadeOut();
          return false;
        });

Even though the hide/appear seems to work the form goes hidden if I click on it. 即使隐藏/出现似乎有效,如果我点击它,表单也会被隐藏。 This is a problem as no one can sign up if they can't type/click on the form. 这是一个问题,因为如果他们无法在表单上键入/单击,则无人注册。 (ISSUE FIXED) (问题已修复)

here is the codepen (live version): http://codepen.io/mariomez/pen/WbgrNK 这是codepen(实时版): http ://codepen.io/mariomez/pen/WbgrNK

You are looking for a div with the id "signup", but that is the class name. 您正在寻找ID为“注册”的div,但这是类名。

$(function() {
  $(".signup").hide();
});

Will hide the signup div when the page loads. 将在页面加载时隐藏注册div。

You could then use $(".signup").toggle(); 然后你可以使用$(".signup").toggle(); on the button click event. 在按钮单击事件上。

If you combine these, eg 如果你将这些结合起来,例如

$(function() {
  $(".signup").hide();

  $(".button").click(function() {
    $(".signup").toggle();
  });
});

Then the button displays the signup block correctly, and it is hidden on page load. 然后该按钮正确显示注册块,并在页面加载时隐藏。

You hide the "signup" Div but try to restore the "modal" div 你隐藏“注册”Div但尝试恢复“模态”div

This works: 这有效:

$('.button').click(function(){
      $('#signup').css('display','block');
      $('.modal-bg').fadeIn();
});

    $('#close').click(function(){
          $('.modal-bg').fadeOut();     
          $('#signup').toggle();
      return false;
    });

You don't need the signup div at all - I think it complicates things. 你根本不需要注册div - 我认为它使事情变得复杂。 Just start your modal background hidden so before your button click code add this: 只需启动隐藏的模态背景,然后在按钮单击代码之前添加:

$('.modal-bg').css('display','none');       

Example

Update 更新

to stop your form showing up whilst your page loads I would add a js div: 在页面加载时停止显示表单我会添加一个js div:

after your body opening tag: 在你的身体打开标签后:

<script>
   document.write('<div class="js">');
</script>

before your body closing tag: 在你的身体结束标记之前:

<script>
   document.write('</div>');
</script>

This allows you to apply js only style, in this case for the modal-bg: 这允许您仅应用js样式,在本例中为modal-bg:

.js .modal-bg {display:none;}

and then you can remove the above js to hide the modal as it will be now in your css 然后你可以删除上面的js隐藏模态,因为它现在将在你的CSS中

updated codepen 更新的codepen

Here is the jQuery to change the CSS on page load: 这是在页面加载时更改CSS的jQuery:

$('.signup').css({
  'display': 'none'
});

First method (CSS) - CodePen 第一种方法(CSS) - CodePen

#signup {

     display: none;
}

Second method (jQuery) - CodePen 第二种方法(jQuery) - CodePen

$('#signup').hide();

Looks like you already figured it out in your codepen paste? 看起来你已经在你的codepen粘贴中找到了它? That code is different from your post... 该代码与您的帖子不同...

Anyway, you have 3 layers of divs in your Login dialog: 无论如何,您在“登录”对话框中有3层div:
#signup > modal-bg > modal #signup> modal-bg> modal

So if you want to hide #signup after loading the page: 因此,如果您想在加载页面后隐藏#signup:

$(function() {
  $("#signup").hide();
});

then you also need to show/hide #signup in your button events. 那么你还需要在按钮事件中显示/隐藏#signup。

.modal-bg and #modal are child elements of #signup, so if you remain #signup in hidden, .modal-bg and #modal will always be hidden no matter what you do on them. .modal-bg和#modal是#signup的子元素,所以如果你保持隐藏的#signup,无论你对它们做什么,都会隐藏.modal-bg和#modal。

By the way, I suggest you to use FireBug or general developer tools (hit F12 in chrome/IE/FIrefox) to inspect HTML elements and check what goes wrong, at least that's how I found the problem in your code. 顺便说一句,我建议你使用FireBug或一般开发人员工具(在chrome / IE / FIrefox中点击F12)来检查HTML元素并检查出错的地方,至少我在代码中发现了问题。

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

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