javascript - 单击时多次调用 Ready 函数

标签 javascript jquery drupal jquery-ui-dialog

我使用以下代码将未排序的 html 列表转换为选择下拉列表:

jQuery(document).ready( function($) {

//build dropdown - main navigation
$("<select />").appendTo(".region-menu-inner nav");

// Create default option "Go to..."
$("<option />", {
   "selected": "selected",
   "value"   : "",
   "text"    : "Navigate..."
}).appendTo("nav select");  

// Populate dropdowns with the first menu items
$(".region-menu-inner li a").each(function() {
    var el = $(this);
    $("<option />", {
        "value"   : el.attr("href"),
        "text"    : el.text()
    }).appendTo(".region-menu-inner select");
});

//make responsive dropdown menu actually work           
$(".region-menu-inner select").change(function() {
    window.location = $(this).find("option:selected").val();
});

}); 

同时,我正在使用 Drupal 的简单对话框模块来创建模块化窗口。该模块仅附带一个 js 文件。该模块使用的代码如下:

/*
  @file
  Defines the simple modal behavior
*/

(function ($) {
  /*
    Add the class 'simple-dialog' to open links in a dialog
    You also need to specify 'rev="<selector>"' where the <selector>
    is the unique id of the container to load from the linked page.
    Any additional jquery ui dialog options can be passed through
    the rel tag using the format:
       rel="<option_name1>:<value1>;<option_name2>:<value2>;"
    e.g. <a href="financing/purchasing-options" class="simple-dialog"
          rel="width:900;resizable:false;position:[60,center]"
          rev="content-area" title="Purchasing Options">Link</a>
    NOTE: This method doesn't not bring javascript files over from
    the target page. You will need to make sure your javascript is
    either inline in the html that's being loaded, or in the head tag
    of the page you are on.
    ALSO: Make sure the jquery ui.dialog library has been added to the page
  */
  Drupal.behaviors.simpleDialog = {
    attach: function (context, settings) {
      // Create a container div for the modal if one isn't there already
      if ($("#simple-dialog-container").length == 0) {
        // Add a container to the end of the body tag to hold the dialog
        $('body').append('<div id="simple-dialog-container" style="display:none;"></div>');
        try {
          // Attempt to invoke the simple dialog
          $( "#simple-dialog-container", context).dialog({
            autoOpen: false,
            modal: true,
            close: function(event, ui) {
              // Clear the dialog on close. Not necessary for your average use
              // case, butis useful if you had a video that was playing in the
              // dialog so that it clears when it closes
              $('#simple-dialog-container').html('');
            }
          });
          var defaultOptions = Drupal.simpleDialog.explodeOptions(settings.simpleDialog.defaults);
          $('#simple-dialog-container').dialog('option', defaultOptions);
        }
        catch (err) {
          // Catch any errors and report
          Drupal.simpleDialog.log('[error] Simple Dialog: ' + err);
        }
      }
      // Add support for custom classes if necessary
      var classes = '';
      if (settings.simpleDialog.classes) {
        classes = ', .' + settings.simpleDialog.classes;
      }
      $('a.simple-dialog' + classes, context).each(function(event) {
        if (!event.metaKey && !$(this).hasClass('simpleDialogProcessed')) {
          // Add a class to show that this link has been processed already
          $(this).addClass('simpleDialogProcessed');
          $(this).click(function(event) {
            // prevent the navigation
            event.preventDefault();
            // Set up some variables
            var url = $(this).attr('href');
            // Use default title if not provided
            var title = $(this).attr('title') ? $(this).attr('title') : settings.simpleDialog.title;
            if (!title) {
                title = $(this).text();
            }
            // Use defaults if not provided
            var selector = $(this).attr('name') ? $(this).attr('name') : settings.simpleDialog.selector;
            var options = $(this).attr('rel') ? Drupal.simpleDialog.explodeOptions($(this).attr('rel')) : Drupal.simpleDialog.explodeOptions(settings.simpleDialog.defaults);
            if (url && title && selector) {
              // Set the custom options of the dialog
              $('#simple-dialog-container').dialog('option', options);
              // Set the title of the dialog
              $('#simple-dialog-container').dialog('option', 'title', title);
              // Add a little loader into the dialog while data is loaded
              $('#simple-dialog-container').html('<div class="simple-dialog-ajax-loader"></div>');
              // Change the height if it's set to auto
              if (options.height && options.height == 'auto') {
                $('#simple-dialog-container').dialog('option', 'height', 200);
              }
              // Use jQuery .get() to request the target page
              $.get(url, function(data) {
                // Re-apply the height if it's auto to accomodate the new content
                if (options.height && options.height == 'auto') {
                  $('#simple-dialog-container').dialog('option', 'height', options.height);
                }
                // Some trickery to make sure any inline javascript gets run.
                // Inline javascript gets removed/moved around when passed into
                // $() so you have to create a fake div and add the raw data into
                // it then find what you need and clone it. Fun.
                $('#simple-dialog-container').html( $( '<div></div>' ).html( data ).find( '#' + selector ).clone() );
                // Attach any behaviors to the loaded content
                Drupal.attachBehaviors($('#simple-dialog-container'));
              });
              // Open the dialog
              $('#simple-dialog-container').dialog('open');
              // Return false for good measure
              return false;
            }
          });
        }
      });
    }
  }

  // Create a namespace for our simple dialog module
  Drupal.simpleDialog = {};

  // Convert the options to an object
  Drupal.simpleDialog.explodeOptions = function (opts) {
    var options = opts.split(';');
    var explodedOptions = {};
    for (var i in options) {
      if (options[i]) {
        // Parse and Clean the option
        var option = Drupal.simpleDialog.cleanOption(options[i].split(':'));
        explodedOptions[option[0]] = option[1];
      }
    }
    return explodedOptions;
  }

  // Function to clean up the option.
  Drupal.simpleDialog.cleanOption = function(option) {
    // If it's a position option, we may need to parse an array
    if (option[0] == 'position' && option[1].match(/\[.*,.*\]/)) {
      option[1] = option[1].match(/\[(.*)\]/)[1].split(',');
      // Check if positions need be converted to int
      if (!isNaN(parseInt(option[1][0]))) {
        option[1][0] = parseInt(option[1][0]);
      }
      if (!isNaN(parseInt(option[1][1]))) {
        option[1][1] = parseInt(option[1][1]);
      }
    }
    // Convert text boolean representation to boolean
    if (option[1] === 'true') {
      option[1]= true;
    }
    else if (option[1] === 'false') {
      option[1] = false;
    }
    return option;
  }

  Drupal.simpleDialog.log = function(msg) {
    if (window.console) {
      window.console.log(msg);
    }

  }

})(jQuery);

使用此模块的链接,在源代码中如下所示:

<a href='/user' name='user-login' id='user-login' class='simple-dialog' title='Login '  rel='width:400;resizable:false;position:[center,60]'>Log in</a>

问题是,当您单击该链接时,需要一两秒钟的时间来加载弹出窗口,而当它实际加载时,会生成第二组选择下拉列表。如果您再次单击登录链接,则会生成第三个选择列表。基本上它会复制从 ul li 转换为选择列表的任何内容。

提前感谢您的帮助。

最佳答案

jQuery(document).ready( function($) {
    $(".region-menu-inner nav").empty(); //empty here
   //build dropdown - main navigation
   $("<select />").appendTo(".region-menu-inner nav");

   // Create default option "Go to..."
    $("<option />", {
      "selected": "selected",
      "value"   : "",
      "text"    : "Navigate..."
    }).appendTo("nav select");  

  // Populate dropdowns with the first menu items
 $(".region-menu-inner li a").each(function() {
    var el = $(this);
    $("<option />", {
       "value"   : el.attr("href"),
       "text"    : el.text()
    }).appendTo(".region-menu-inner select");
 });

//make responsive dropdown menu actually work           
 $(".region-menu-inner select").change(function() {
     window.location = $(this).find("option:selected").val();
 });

}); 

关于javascript - 单击时多次调用 Ready 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16555330/

相关文章:

php - 在 Drupal 页面中有选择地删除样式表

drupal - 匿名用户 : You are not authorized to access this page. 权限正常

javascript - 如何从生成的 JSON 数组中提取 JSON 对象?

javascript - MIME类型为application/json的跨域读取阻止(CORB)

jquery - 添加组件或升级自定义 jquery ui 安装

javascript - 是否可以访问和读取 Electron 应用程序的本地存储

javascript - 如何以美观的方式将 <select> 和 &lt;input&gt; 结合起来?

javascript - 如果已经触发,则仅触发 Jquery ajax 一次

javascript - JQuery 计算器数据数字不工作|让 allclear 按钮工作

javascript - 如何在jquery中添加url路径