【问题标题】:SimpleModal -- Open OnLoadSimpleModal -- 打开 OnLoad
【发布时间】:2010-02-16 15:22:11
【问题描述】:

我是 JQuery 的新手——对 javascript 并不陌生。

能够使用演示 index.html 页面中提供的超链接按钮打开 OSX 样式对话框,但希望在页面加载时打开它。我在 StackOverflow (How do I invoke a SimpleModal OSX dialog on page load?) 上阅读了几个链接,但仍然无法让它在完全相同的 index.html 页面中工作。我最终通过编程调用隐藏按钮元素的按钮单击来采取权宜之计——请参见以下片段:

onLoad="document.getElementById('load_OSX_Example').click();">
<input type="hidden" id="load_OSX_Example" value="" class='osx'>

<script type="text/javascript" language="javascript">
    //#### open the OSX Modal  ##########
    $(document).ready(function(){
       $("#osx-modal-content").modal();
    });
</script>

所以我有两个问题:

  1. 如何以编程方式使用 javascript/html 调用 class='osx'?
  2. 为什么不能使用 $("#osx-modal-content").modal();在javascript中调用(见上面的片段)?我在多个浏览器中尝试过,屏幕上显示的唯一内容是这个标签的内容:“div id="osx-modal-title",并且在jscript控制台中没有错误。

【问题讨论】:

  • @jmein - 你为什么要删除问题的重要部分?请不要编辑,除非您已阅读该问题,它引用了您要删除的 javascript 评论。
  • @Nick 我正在尝试修复代码缩进,但我还没有完成。出于某种原因,在他的问题下方有代码块,它不会让它出现在代码块中,我试图找出原因。所以我只是把它移到上面,并在他的问题中反映出来
  • @jmein - 看看之前的几个版本,当在一个列表下方时,列表和代码之间没有任何内容,需要 8 个空格缩进......我同意有点烦人。

标签: jquery plugins onload simplemodal


【解决方案1】:

对于#1:

$(".osx").click();

对于 #2,这是脚本:

<script type="text/javascript" language="javascript">
$(function() {
  $("#osx-modal-content").modal({
    overlayId: 'osx-overlay',
    containerId: 'osx-container',
    closeHTML: '<div class="close"><a href="#" class="simplemodal-close">x</a></div>',
    minHeight:80,
    opacity:65, 
    position:['0',],
    overlayClose:true,
    onOpen:function (d) {
      var self = this;
      self.container = d.container[0];
      d.overlay.fadeIn('slow', function () {
        $("#osx-modal-content", self.container).show();
        var title = $("#osx-modal-title", self.container);
        title.show();
        d.container.slideDown('slow', function () {
          setTimeout(function () {
            var h = $("#osx-modal-data", self.container).height()
              + title.height()
              + 20; // padding
            d.container.animate(
              {height: h}, 
              200,
              function () {
                $("div.close", self.container).show();
                $("#osx-modal-data", self.container).show();
              }
            );
          }, 300);
        });
      })
    },
    onClose:function (d) {
      var self = this;
      d.container.animate(
        {top:"-" + (d.container.height() + 20)},
        500,
        function () {
          self.close(); // or $.modal.close();
        }
      );
    }
  });
});
</script>

确保包含示例中包含的图像/CSS 以获得样式。

【讨论】:

  • 感谢尼克的回复,#1 有效。至于第 2 点,我没有修改作为示例 (ericmmartin.com/projects/simplemodal-demos) 的一部分下载的 index.html——我所做的只是添加 javascript 片段以打开模式。一定是这里发生了其他事情。
  • @Bill - 使用合并样本更新,使用 $(function() {(与 $(document).ready(function() { 相同)获取加载模式。
  • Nick,您提供的第二个示例在示例中提供的 index.html 中完美运行,谢谢。
【解决方案2】:

比尔,

如果您希望在页面加载时打开对话框,有几个选项。您可以直接编辑 osx.js 文件,也可以在加载 osx.js 文件后添加以下内容:

<script type='text/javascript'>
jQuery(function ($) {
    $("#osx-modal-content").modal({
        overlayId: 'osx-overlay',
        containerId: 'osx-container',
        closeHTML: '<div class="close"><a href="#" class="simplemodal-close">x</a></div>',
        minHeight:80,
        opacity:65, 
        position:['0',],
        overlayClose:true,
        onOpen:OSX.open,
        onClose:OSX.close
    });
});
</script>

【讨论】:

  • 埃里克,感谢您的回复。我尝试了您在 OSX 示例的 index.html 中包含的示例,但是,我不断收到 JS 错误 OSX 未定义。我什至将 osx.js 文件放入头部,但仍然出现错误。然而,下面尼克的示例确实加载了示例——我真的很喜欢 OSX 对话框的工作方式,感谢提供它.....这是我第一次涉足 JQuery,我将不得不开始更深入地挖掘进入它的工作方式。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多