为禁用的字段和按钮启用jQueryUI工具提示

时间:2019-08-08 18:00:27

标签: jquery-ui jquery-ui-tooltip

我也想让jQueryUI工具提示也适用于禁用的按钮和输入。我该如何做到这一点?

其中包括一个示例,其中显示了启用和禁用的各种输入和按钮,以及jQueryUI工具提示似乎如何跳过对禁用元素的评估。

请单击字段/按钮标签以打开工具提示。我的用户不喜欢基于INPUT的悬停工具提示,并且悬停在移动设备上不起作用。

如果将鼠标悬停在禁用的INPUT和BUTTON上,将显示浏览器工具提示,但不显示jQueryUI版本。这很明显,因为浏览器版本不评估HTML,而是原始显示。

注意:此问题不是Show tooltip for disabled items的重复,因为该问题并未询问实际的禁用标签(按钮或输入)

// Disable HOVER tooltips for input elements since they are just annoying.
$("input[title]").tooltip({
  disabled: true,
  content: function() {
    // Allows the tooltip text to be treated as raw HTML.
    return $(this).prop('title');
  },
  close: function(event, ui) {
    // Disable the Tooltip once closed to ensure it can only open via click.
    $(this).tooltip('disable');
  }
});

// Basic tooltips for the Buttons only but format HTML.
$("button[title]").tooltip({
  content: function() {
    // Allows the tooltip text to be treated as raw HTML.
    return $(this).prop('title');
  }
});


/* Manually Open the Tooltips */
$(".ui-field-help").click(function(e) {
  var forId = e.target.getAttribute('for');
  if (forId) {
    var $forEl = $("#" + forId);
    if ($forEl.length)
      $forEl.tooltip('enable').tooltip('open');
  }
});

// The following is only to load the CSS....
function loadCSS(filename) {

  var file = document.createElement("link");
  file.setAttribute("rel", "stylesheet");
  file.setAttribute("type", "text/css");
  file.setAttribute("href", filename);
  document.head.appendChild(file);

}

loadCSS("https://code.jquery.com/ui/1.12.1/themes/start/jquery-ui.css");
.ui-field-help {
  text-decoration: underline;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>

<table width=100%>
  <tr>
    <td><label for="A000" class="ui-field-help">Enabled Input</label></td>
    <td><input type="Text" id="A000" title="title @A000<hr>Fancy <i>HTML</i>..."></td>
  </tr>
  <tr>
    <td><label for="B000" class="ui-field-help">Disabled Input</label></td>
    <td><input disabled=disabled type="Text" id="B000" title="title @B000<hr>Fancy <i>HTML</i>..."></td>
  </tr>
  <tr>
    <td><label for="E000" class="ui-field-help">Enabled Button</label></td>
    <td><button id="E000" title="title @E000<hr>Fancy <i>HTML</i>...">Enabled Button</button></td>
  </tr>
  <tr>
    <td><label for="D000" class="ui-field-help">Disabled Button</label></td>
    <td><button disabled=disabled type="Text" id="D000" title="title @D000<hr>Fancy <i>HTML</i>...">Disabled Button</button></td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

找不到答案后,我通过用[title]遍历所有禁用项并单独处理以将工具提示移至该输入元素的标签来解决了该问题。

$("[title]:disabled", $container).each(function (ix, el) {
    // Since the jQueryUI tooltips don't work on disabled controls we remove them 
    // and then attempt to add them to the elements label.
    var title = el.title;
    el.title = ''; // Clear the title to avoid the browser tooltip

    // Look for a Label attached to the element and add the tooltip there.
    $('label[for=' + el.id + ']').attr('title', title).tooltip({
        content: function () {
            // Allows the tooltip text to be treated as raw HTML.
            return $(this).prop('title');
        }
    });
});
相关问题