Jquery模式不能正常工作

时间:2018-02-12 04:30:13

标签: javascript jquery jquery-ui jquery-ui-dialog

我正在尝试使用jquery ui模态,但它不能满足我的需求,即,我想仅在模态内显示数据,但是在我点击按钮之前显示它。



function pop_up()
{
var dialog, form
        dialog = $('div#infoDialog').dialog({
          autoOpen: false,
          height: 600,
          width: 500,
          modal: true
        });
        $('#showInfos').click(function(e){
          e.preventDefault();
          dialog.dialog('open');
        });
}

  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/redmond/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

    <button type="button" id="showInfos" onclick="pop_up();">test</button>
    <div id="infoDialog" title="Eventinfos">
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
      </p>
    </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您的代码几乎是正确的,您的代码中只有一个错误,那就是您将所有javascript代码与onclick事件捆绑在一起,这就是默认显示模式内容的原因。         所以现在你只需要删除onclick事件和pop_up()函数。所以你的最终代码如下所示。

&#13;
&#13;
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
 
<script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>

<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" />

    <button type="button" id="showInfos" >test</button>
    <div id="infoDialog" title="Eventinfos">
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
        
      </p>
    </div>

    <script>
   // function pop_up()
  // {
  var dialog, form
          dialog = $('div#infoDialog').dialog({
            autoOpen: false,
            height: 600,
            width: 500,
            modal: true
          });
          $('#showInfos').click(function(e){
            e.preventDefault();
            dialog.dialog('open');
          });
  // }
    </script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用DOM ready事件在页面加载时设置模态一次更好: (更多信息:https://learn.jquery.com/using-jquery-core/document-ready/

$(document).ready(function(){
    $('div#infoDialog').dialog({
       autoOpen: false, // this should be false unless you want it opened from the start
       height: 600,
       width: 500,
       modal: true
    });

    //now let's bind the click event:
    $('#showInfos').click(function(){
       $('div#infoDialog').dialog('open');
    });
})

现在你的html变成了,请注意按钮上没有onclick=""

<button type="button" id="showInfos">test</button>
    <div id="infoDialog" title="Eventinfos">
      <p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
      </p>
    </div>

小提琴:https://jsfiddle.net/4hxd5tLL/1/