MAO-EYE 2009-08-25 14:37 采纳率: 100%
浏览 410
已采纳

如何使用 jQuery 生成一个简单的弹出窗口

I am designing a web page. When we click the content of div named mail, how can I show a popup window containing a label email and text box?

转载于:https://stackoverflow.com/questions/1328723/how-to-generate-a-simple-popup-using-jquery

  • 写回答

11条回答 默认 最新

  • 程序go 2009-08-25 17:03
    关注

    First the CSS - tweak this however you like:

    a.selected {
      background-color:#1F75CC;
      color:white;
      z-index:100;
    }
    
    .messagepop {
      background-color:#FFFFFF;
      border:1px solid #999999;
      cursor:default;
      display:none;
      margin-top: 15px;
      position:absolute;
      text-align:left;
      width:394px;
      z-index:50;
      padding: 25px 25px 20px;
    }
    
    label {
      display: block;
      margin-bottom: 3px;
      padding-left: 15px;
      text-indent: -15px;
    }
    
    .messagepop p, .messagepop.div {
      border-bottom: 1px solid #EFEFEF;
      margin: 8px 0;
      padding-bottom: 8px;
    }
    

    And the JavaScript:

    function deselect(e) {
      $('.pop').slideFadeToggle(function() {
        e.removeClass('selected');
      });    
    }
    
    $(function() {
      $('#contact').on('click', function() {
        if($(this).hasClass('selected')) {
          deselect($(this));               
        } else {
          $(this).addClass('selected');
          $('.pop').slideFadeToggle();
        }
        return false;
      });
    
      $('.close').on('click', function() {
        deselect($('#contact'));
        return false;
      });
    });
    
    $.fn.slideFadeToggle = function(easing, callback) {
      return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback);
    };
    

    And finally the html:

    <div class="messagepop pop">
      <form method="post" id="new_message" action="/messages">
        <p><label for="email">Your email or name</label><input type="text" size="30" name="email" id="email" /></p>
        <p><label for="body">Message</label><textarea rows="6" name="body" id="body" cols="35"></textarea></p>
        <p><input type="submit" value="Send Message" name="commit" id="message_submit"/> or <a class="close" href="/">Cancel</a></p>
      </form>
    </div>
    
    <a href="/contact" id="contact">Contact Us</a>
    

    Here is a jsfiddle demo and implementation.

    Depending on the situation you may want to load the popup content via an ajax call. It's best to avoid this if possible as it may give the user a more significant delay before seeing the content. Here couple changes that you'll want to make if you take this approach.

    HTML becomes:

    <div>
        <div class="messagepop pop"></div> 
        <a href="/contact" id="contact">Contact Us</a>
    </div>
    

    And the general idea of the JavaScript becomes:

    $("#contact").on('click', function() {
        if($(this).hasClass("selected")) {
            deselect();               
        } else {
            $(this).addClass("selected");
            $.get(this.href, function(data) {
                $(".pop").html(data).slideFadeToggle(function() { 
                    $("input[type=text]:first").focus();
                });
            }
        }
        return false;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并