jquery - 当使用 jQuery UI 激活工具提示时,按钮单击不会在警报中显示标题

标签 jquery css tooltip buttonclick

我正在尝试使用 jQuery 显示警报,单击按钮即可显示警报中标题属性的值。我无法显示它,警报消息显示一个空字符串。 Here is my trails

单击#btn1 显示空标题。为什么点击时标题属性会被清除?

$(document).tooltip 是否会产生一些影响?我删除了 $(document).tooltip 函数并且它正在工作。我正在尝试使其与 jQuery UI 工具提示一起使用。

<p>Your age:
    <input type=text title="We ask for your age only for statistical purposes.">
</p>
<p>
    <input type="button" id="btn1" title="This a test enabled button." value="hover me please">
</p>
<p>
    <input type="button" disabled="disabled" id="btn2" title="This a test disabled button." value="hover me please">
</p>

$(function () {
    $(document).on("click","#btn1", function () {
        alert($(this).attr("title"));
    });

    $(document).on("click","#btn2", function () {
        alert($(this).attr("title"));
    });

 $(document).tooltip({
        position: {
            my: "center bottom-20",
            at: "center top",
            using: function (position, feedback) {
                $(this).css(position);
                $("<div>")
                    .addClass("arrow")
                    .addClass(feedback.vertical)
                    .addClass(feedback.horizontal)
                    .appendTo(this);
            }
        }
    });
});

最佳答案

上线 jQuery UI 第 16182 行(ctrl-F for "target.removeAttr( "title");")您可以看到 title 属性已被删除。此行上方的注释准确地解释了这样做的原因。

以下是 jQuery UI 文件中的注释,解释了为什么删除 title 属性的值。

// if we have a title, clear it to prevent the native tooltip  
// we have to check first to avoid defining a title if none exists  
// (we don't want to cause an element to start matching [title])  
//  
// We use removeAttr only for key events, to allow IE to export the correct  
// accessible attributes. For mouse events, set to empty string to avoid  
// native tooltip showing up (happens only when removing inside mouseover).  

Here is a work around

var btn1_title_at_load = $("#btn1").attr("title");
$(document).on("click","#btn1", function () {
    alert(btn1_title_at_load);
});

Here is an alternitive work around .

<input type="button" id="btn1" title="This a test enabled button." 
       value="hover me please" 
       data-title="This a test enabled button." />

$(document).on("click","#btn1", function () {
    var t = $(this).data("title");
    window.alert(t);
});

支持证据。

Programmatically click the button on page load 请注意,悬停事件在页面加载时未激活。

关于jquery - 当使用 jQuery UI 激活工具提示时,按钮单击不会在警报中显示标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26153220/

相关文章:

jquery - 两个 "or"事件条件?

javascript - 使用 CSS 打印 DIV 内容,不起作用

tooltip - 图表上任意位置的 Chart.js 工具提示

css - Bootstrap.css 和 NVD3 的 css 好像有冲突,导致没有工具提示。我可以在我的网页上同时使用两者吗?

javascript - Ajax 时无法离开页面(异步)

c# - ASP.NET 双列表框使用 JQuery 更新了客户端,以及如何在服务器端检索结果

javascript - 我在使用 fancybox 插件时遇到问题

javascript - YUI 工具提示未显示在面板顶部

javascript - 使菜单上下滑动

javascript - Bootstrap 表单重复内容