简单的模态问题 - 多个、消失的内容

发布于 2024-08-09 21:57:46 字数 801 浏览 9 评论 0原文

我是一名 jQuery 新手,通过在我的脚本中执行此操作,我已经操纵了简单模态,以允许我在页面上拥有多个模态:

$('input.basic, a.basic').click(function ( e) { e.preventDefault(); $(this).next('.basicModalContent').modal(); });

这是我的 HTML:

    <a class="basic linkHeading" href="#">Link Heading</a>

 <div class="basicModalContent" style="display: none;">
    <h1>This Resource Requires Login</h1>
    <a href="#" class="simplemodal-close" title="Close">Cancel</a></p>
</div><!--basicModal-->

我遇到的问题是第一次点击后一切正常。关闭。第二次单击启动模式,但所有内容都从框中消失。

请参阅此链接以了解正在发生的错误: http://blanksky.com/test/ebenefits21/modal .html

I'm a jQuery newbie and have rigged Simple Modal to allow me to have more than one modal on a page by doing this in my script:

$('input.basic, a.basic').click(function (e) {
e.preventDefault();
$(this).next('.basicModalContent').modal();
});

here's my HTML:

    <a class="basic linkHeading" href="#">Link Heading</a>

 <div class="basicModalContent" style="display: none;">
    <h1>This Resource Requires Login</h1>
    <a href="#" class="simplemodal-close" title="Close">Cancel</a></p>
</div><!--basicModal-->

The issue I'm running into is everything works fine on first click & close. The second click launches the modal, but all the content is gone from inside the box.

see this link for the bug in action: http://blanksky.com/test/ebenefits21/modal.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜光 2024-08-16 21:57:46

我建议类似:

链接:

<a href="#" class="basic" id="link-1">link1</a>
<a href="#" class="basic" id="link-2">link2</a>
<a href="#" class="basic" id="link-3">link3</a>

隐藏内容(通过CSS或内联样式)

<div id="link-1-content" style="display:none">
<p>content</p>
</div>
<div id="link-2-content" style="display:none">
<p>content</p>
</div>
<div id="link-3-content" style="display:none">
<p>content</p>
</div>

JavaScript:

$(document).ready(function () {
      $('#basic-modal input.basic, #basic-modal a.basic').click(function (e) {
              e.preventDefault();
              var content = '#' + this.id + '-content';
              $(content).modal();
      });
});

类似的东西应该可以解决问题。

I would suggest something like:

Links:

<a href="#" class="basic" id="link-1">link1</a>
<a href="#" class="basic" id="link-2">link2</a>
<a href="#" class="basic" id="link-3">link3</a>

Hidden content (via CSS or inline style)

<div id="link-1-content" style="display:none">
<p>content</p>
</div>
<div id="link-2-content" style="display:none">
<p>content</p>
</div>
<div id="link-3-content" style="display:none">
<p>content</p>
</div>

JavaScript:

$(document).ready(function () {
      $('#basic-modal input.basic, #basic-modal a.basic').click(function (e) {
              e.preventDefault();
              var content = '#' + this.id + '-content';
              $(content).modal();
      });
});

Something like that should do the trick.

如此安好 2024-08-16 21:57:46

当你隐藏对话框时,jQuery改变了div在DOM中的位置,然后想用“$(this).next(...)”来定位它,你不能这样做。您应该有一些 ID 或参考资料,以便以其他方式查找。

编辑:
好吧,你让我的想法有点回响。我希望我正在经历的代码有用。我没有经过测试:

$('input.basic, a.basic').click(function (e) 
{ 
   e.preventDefault(); 
   var el = "";

   if($(this).data("xid")!=undefined)
   {
      el = $($(this).data("xid"));
   }
   else
   {
     var xid = "xid_" + ((new Date()).getTime());
     el = $(this).next('.basicModalContent');
     $(this).data("xid", xid);
     if (el.lenght>0)
       el.attr("id", xid);
   }

   if (el.lenght>0)
   {
     el.modal();
   }
});

When you hide the dialog, jQuery changes the position of the div in the DOM, then wanting to locate it with "$(this).next(...)", you can not do. You should have some ID or a reference to locate otherwise.

EDIT:
Well, you have me thinking a little echo. I hope it is useful the code that I'm going through. I'm not tested:

$('input.basic, a.basic').click(function (e) 
{ 
   e.preventDefault(); 
   var el = "";

   if($(this).data("xid")!=undefined)
   {
      el = $($(this).data("xid"));
   }
   else
   {
     var xid = "xid_" + ((new Date()).getTime());
     el = $(this).next('.basicModalContent');
     $(this).data("xid", xid);
     if (el.lenght>0)
       el.attr("id", xid);
   }

   if (el.lenght>0)
   {
     el.modal();
   }
});
十六岁半 2024-08-16 21:57:46

需要较少标记的解决方案是使用单个模式“窗口”。请参见以下示例:


<!doctype html>
<html>
<head>
 <meta charset="utf-8" />
 <title>Simple Modal Demo</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
 <script type="text/javascript" src="simplemodal.js"></script>
 <script type="text/javascript">
  $(document).ready(
    function() {
     $('.loginRequired').bind('click',
      function() {
       $('#clickedLink').html(this.id);
       $('#modalWindow').modal();
       return false;
      }
     );
    }
  );
 </script>

 <style>
  #simplemodal-container { display: none; }
  #simplemodal-overlay { background: #000; opacity: 0.4; }
  #modalWindow {
   background: #FFF;
   border: 1px solid black;
   height: 100px;
   opacity: 1.0;
   padding: 10px;
   position: relative;
   width: 220px;
   z-index: 1010;
  }
 </style>
</head>
<body>
 <p><a href="http://example.com/" id="link1" class="loginRequired">Login Required Link</a></p>
 <p><a href="http://example.com/" id="link2" class="loginRequired">Login Required Link</a></p>
 <p><a href="http://example.com/" id="link3" class="loginRequired">Login Required Link</a></p>
 <p><a href="http://example.com/">http://example.com/</a></p>

 <div id="simplemodal-container">
  <div id="modalWindow">
   <p>Sorry, you must login to click <b id="clickedLink">unknown</b>.</p>
   <p><a href="#" class="simplemodal-close">Cancel</a></p>
  </div>
 </div>

</body>
</html>

A solution that requires less markup is to use a single modal "window." See the following example:


<!doctype html>
<html>
<head>
 <meta charset="utf-8" />
 <title>Simple Modal Demo</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
 <script type="text/javascript" src="simplemodal.js"></script>
 <script type="text/javascript">
  $(document).ready(
    function() {
     $('.loginRequired').bind('click',
      function() {
       $('#clickedLink').html(this.id);
       $('#modalWindow').modal();
       return false;
      }
     );
    }
  );
 </script>

 <style>
  #simplemodal-container { display: none; }
  #simplemodal-overlay { background: #000; opacity: 0.4; }
  #modalWindow {
   background: #FFF;
   border: 1px solid black;
   height: 100px;
   opacity: 1.0;
   padding: 10px;
   position: relative;
   width: 220px;
   z-index: 1010;
  }
 </style>
</head>
<body>
 <p><a href="http://example.com/" id="link1" class="loginRequired">Login Required Link</a></p>
 <p><a href="http://example.com/" id="link2" class="loginRequired">Login Required Link</a></p>
 <p><a href="http://example.com/" id="link3" class="loginRequired">Login Required Link</a></p>
 <p><a href="http://example.com/">http://example.com/</a></p>

 <div id="simplemodal-container">
  <div id="modalWindow">
   <p>Sorry, you must login to click <b id="clickedLink">unknown</b>.</p>
   <p><a href="#" class="simplemodal-close">Cancel</a></p>
  </div>
 </div>

</body>
</html>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文