【问题标题】:Jquery UI Tooltip not able to hideJquery UI Tooltip 无法隐藏
【发布时间】:2017-02-23 18:14:06
【问题描述】:

我有两个按钮 +- 。当我点击 + 按钮时,我在父 div 中添加一个 div 并希望在我再次点击 + 按钮时在该 div 上显示工具提示,然后我再次添加一个 div 并只想在第二个 div 上显示工具提示。最多 7 个 div 也是如此。

目前我也在做同样的事情,但是当我添加多个 div 工具提示时,不会隐藏上一个按钮,并且仍然会存在。所以最后我看到了所有 7 个工具提示,但我应该只看到最后一个 div 工具提示。

经过一段时间的延迟,我试图隐藏它,但即使它不起作用。

这是我的代码。

function init(){
for(var divCount = 1; divCount <= fanSpeed; divCount++){
    $('#temperature_bar').append('<div style="float:left" data-tooltip-id="'+divCount+'" id="speed_"'+divCount +'" class="trigger climate-fan-bar" onclick="\buttonTempraturePressed(this.id)\" title="<div class="row row-thumbnail" id="box-search"><div class="thumbnail text-center"><img width="50" height="50" src="images/fan_speed_bg_center_popup.png" class="img-responsive"><div class="caption"><span class="tooltip-text-size">' + divCount + '</span></div></div></div></div>' ); 
    showTooltips("speed_"+divCount);
}
}

function showTooltips(clicked_id){
        //show
        $(clicked_id).on('click', '.trigger', function () {
            $(this).addClass("on");
                $(this).tooltip({
                hide: { effect: "flip", duration: 1000 },
                items: '.trigger.on',                   
                position: {
                    my: "top",
                    at: "top",
                    collision: "flip"
                }                   
            });
            $(this).trigger('mouseenter');
        });         



        /*$(clicked_id).on('click', '.trigger.on', function () {
            $(this).tooltip('close');
            $(this).removeClass("on");
        });*/
        //prevent mouseout and other related events from firing their handlers
        $(".trigger").on('mouseout', function (e) {
            e.stopImmediatePropagation();
        });
    }

【问题讨论】:

  • 你能发布一个活生生的例子/小提琴吗?
  • 我无法创建小提琴,但我更新了我的代码。请通过它
  • 没有 jsfiddle 很难调试。我至少可以说onclick="\buttonTempraturePressed(this.id)\" 应该替换为onclick="buttonTempraturePressed(this.id);"
  • 另外,将" id="speed_" 替换为" id="speed_(结尾" 稍后在您的串联中提供)。
  • title="&lt;div ... 部分,一些双引号可能应该被转义。我不知道是哪些,因为我不太清楚应该包含在该属性中的内容。您可以尝试简化该部分,然后用最少的代码制作一个 jsfiddle。

标签: html jquery-ui hide jquery-ui-tooltip


【解决方案1】:

这是一个可能的解决方案。利用itemscontent 选项,您可以调整内容而不是尝试通过title 属性传递它。

工作示例:https://jsfiddle.net/Twisty/cfd6z8db/

HTML

<div style="padding:20px; display: block;">
  <div id="temperature_bar">
  </div>
</div>
<div style="padding:20px; display: block;">
  <label>Speed</label>
  <button id="decSpeed" title="Decrease">-</button>
  <button id="incSpeed" title="Increase">+</button>
</div>

CSS

.item {
  border-style: solid;
  border-width: 1px 1px 1px 0;
  border-color: #ccc;
  padding: .25em .5em;
  cursor: pointer;
}

.item:nth-child(1) {
  border-left-width: 1px;
}

.row-thumbnail .caption {
  width: 100%;
  text-align: center;
}

jQuery

function showToolTips(obj) {
  if (obj.hasClass("on")) {
    // Hide ToolTip
    obj.removeClass("on");
    obj.tooltip("close");
  } else {
    // Show ToolTip
    obj.addClass("on");
    obj.tooltip("open");
  }
}

function addItem() {
  var divCount = $(".item").length + 1;
  if (divCount == 8) {
    return false;
  }
  var img = "https://dummyimage.com/50x50/000000/fff";
  var item = $("<div>", {
      id: "speed-" + divCount,
      class: "trigger item",
      "data-tip-content": img + "|" + divCount,
      "data-tip-id": divCount,
    })
    .css("float", "left");
  item.html(divCount);
  item.tooltip({
    disabled: true,
    hide: {
      effect: "flip",
      duration: 1000
    },
    position: {
      my: "center bottom+5",
      at: "center top",
      collision: "flip"
    },
    items: "[data-tip-content]",
    content: '<div class="row row-thumbnail" id="box-search"><div class="thumbnail text-center"><img width="50" height="50" src="' + item.data("tip-content").split("|")[0] + '" class="img-responsive"><div class="caption"><span class="tooltip-text-size">' + item.data("tip-content").split("|")[1] + '</span></div></div>'
  });
  $('#temperature_bar').append(item);

}

// Make Items
function init() {
  var c = 5;
  var divCount = 1;
  for (divCount; divCount <= c; divCount++) {
    var item = $("<div>", {
        id: "speed-" + divCount,
        class: "trigger item",
        "data-tip-content": "https://dummyimage.com/50x50/000000/fff" + "|" + divCount,
        "data-tip-id": divCount,
      })
      .css("float", "left");
    item.html(divCount);
    item.tooltip({
      disabled: true,
      hide: {
        effect: "flip",
        duration: 1000
      },
      position: {
        my: "center bottom+5",
        at: "center top",
        collision: "flip"
      },
      items: "[data-tip-content]",
      content: '<div class="row row-thumbnail" id="box-search"><div class="thumbnail text-center"><img width="50" height="50" src="' + item.data("tip-content").split("|")[0] + '" class="img-responsive"><div class="caption"><span class="tooltip-text-size">' + item.data("tip-content").split("|")[1] + '</span></div></div>'
    });
    $('#temperature_bar').append(item);
  }
}

$(function() {
  init();
  $(".trigger").on('mouseout', function(e) {
    if ($(this).hasClass("on")) {
      $(this).removeClass("on");
      $(this).tooltip("close");
    }
  });
  $("#temperature_bar").on("click", ".trigger", function() {
    console.log("Click Triggered. Executing 'showToolTips()'.");
    showToolTips($(this));
  });
  $("#decSpeed").click(function() {
    $("#temperature_bar .item").last().remove();
  });
  $("#incSpeed").click(addItem);
});

当使用像{ my: "top", at: "top", collision: "flip" } 这样的位置时,这会导致某种奇怪的加载问题。我使用了不同的位置,它似乎解决了问题。

您的帖子没有给出一个非常强大的示例,所以我创建了一些示例按钮。 disable 选项也很有帮助,这确保了它们不会显示在 mouseover 事件中。它还允许我们控制openclose

我将所有内容项打包成一个数据属性。我用一根管道 (|) 将它分开,这样它就可以全部放在一个位置,但以后可以轻松使用。

单击一个按钮,就会出现提示。再次单击它,它会隐藏。移开鼠标也一样。

如果您需要更多信息,请使用更好的示例更新您的帖子。

【讨论】:

    【解决方案2】:

    我会简单地执行

    $(".ui-tooltip-content").hide(); 
    

    到:

    function showTooltips(clicked_id){
        $(".ui-tooltip-content").hide(); // Clean up prior tips
        //show
        $(clicked_id).on('click', '.trigger', function () {
            $(this).addClass("on");
                $(this).tooltip({
    ...
    

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      相关资源
      最近更新 更多