Lotus@ 2009-08-25 14:37 采纳率: 100%
浏览 972
已采纳

如何使用 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

  • 写回答

10条回答 默认 最新

  • 衫裤跑路 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;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法