javascript - jQuery .click 问题

标签 javascript html jquery jquery-ui jquery-events

我面临着非常奇怪和困难的问题。我正在开发一个网络应用程序,我在其中创建了一些小部件。如果我单击图标,小部件编辑器将出现在模块化弹出窗口中,用户可以输入他的数据,该数据将作为标签显示在仪表板上。我在仪表板上的标签上方有“编辑”按钮,现在我想添加一个删除按钮,假设用户将在仪表板上添加小部件,后来他发现不需要该仪表板,因此他单击删除按钮 该小部件应该被删除,如果用户在删除后觉得他再次想要该小部件,当他单击之前删除的特定小部件的图标时,该小部件应该再次出现。我尝试这样做,但是一旦单击删除按钮,我就可以删除该小部件,但是当我单击小部件的图标时,我无法返回。

请帮我解决这个问题。

最佳答案

只是猜测您要做什么,下面是添加可以再次关闭的 div 的示例:

设置:

var WIDGET_MARKUP =
    "<div class='widget'>I'm the widget. " +
    "<span class='close'>Close</span>" +
    "</div>";

// Opens the widget, returns true; if the widget is already
// open, doesn't open a a second and returns false.
function openWidget(onClose) {
  var widget;

  // If there's already one open, don't do anything
  if ($("div.widget").length > 0) {
    // Already open, don't open another
    return false;
  }

  // There isn't, add one
  widget = $(WIDGET_MARKUP);
  widget.appendTo(document.body);
  widget.delegate('span.close', 'click', function() {
    // Remove the widget
    widget.remove();

    // Call the callback if any
    if (onClose) {
      try {
        onClose();
      }
      catch (e) {
      }
    }
  });

  // true = opened the widget
  return true;
}

用途:

$('#btnAddWidget').click(function() {
  var button = this;

  if (openWidget(handleClose)) {
    // Opened the widget, disable our button
    button.disabled = true;
  }

  function handleClose() {
    // Widget was closd, re-enable the button
    button.disabled = false;
  }
});

Live example

显然,只有当您需要时,禁用该按钮才有意义;如果你不这样做,就这样使用它:

设置:

var WIDGET_MARKUP =
    "<div class='widget'>I'm the widget. " +
    "<span class='close'>Close</span>" +
    "</div>";

// Opens the widget, returns true; if the widget is already
// open, doesn't open a a second and returns false.
function openWidget(onClose) {
  var widget;

  widget = $(WIDGET_MARKUP);
  widget.appendTo(document.body);
  widget.delegate('span.close', 'click', function() {
    // Remove the widget
    widget.remove();

    // Call the callback if any
    if (onClose) {
      try {
        onClose();
      }
      catch (e) {
      }
    }
  });
}

用途:

$('#btnAddWidget').click(function() {
    openWidget();
});

Live example

关于javascript - jQuery .click 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5090580/

相关文章:

带有 Dojo/Dijit 的 Javascript 智能感知是 Visual Studio 2008

javascript - ui-mask 的 Angular js 指令的单元测试

jQuery - 仅将方法应用于一页

html - Angular 5全屏不可移动、不可拉伸(stretch)的背景图

html - CSS仅更改表格中的一行

javascript - jQuery .load()/.ajax() 在附加后不在返回的 HTML 中执行 javascript

Laravel 翻译文件中的 Javascript 调用

Javascript RegExp 非捕获组

jquery - jQtouch 使用 jQuery 而不是 Zepto

javascript - 实现 $(el).trigger ('my-event' , {some : 'data' }); in native js