我有一个为特定对象创建工具提示的函数。目前,我正在ajax插入之后运行一个工具提示函数来创建和附加新的工具提示对象。我很好奇是否有一种方法可以使用.on()在插入时自动运行工具提示函数,而不是手动运行它。
例如:
$('[title]').on('inserted', function(){
tooltip(this);
});
我做了一些阅读,看起来自定义触发器可能是可行的,但我希望它存在这样的东西:)
发布于 2012-05-23 00:13:15
以下是根据请求生成的伪代码。
$(document).ready(function() {
$('body').on('added','*',function() {
console.log($(this),'has been added');
});
$('body').append('<div>This is the first div</div>');
});
(function($) {
fncs = {
append:$.fn.append,
appendTo:$.fn.appendTo
// etc.
}
// we're assigning the original functions in this
// object to be executed (applied) later
$.fn.append = function() {
fncs.append.apply(this,arguments);
$(this).children().last().trigger('added');
return $(this);
}
$.fn.appendTo = function() {
fncs.appendTo.apply(this,arguments);
return $(this);
// no need to trigger because this function calls the one
// above for some reason, and it's taking care of the
// triggering the right element(s I think)
}
})(jQuery);
发布于 2012-05-22 01:40:01
这不是您想要的响应,但我不会将工具提示直接附加到元素上。相反,对于希望工具提示在鼠标悬停时显示的对象,我将使用一个类,并按以下方式使用.on()
事件处理程序:
$('body').on('mouseover','.tooltip',function() {
// show tooltip
console.log($(this).data('tooltip'));
return false;
}).on('mouseout','.tooltip',function() {
// hide tooltip
return false;
});
因此,您添加到主体中的任何内容(不一定是直接的子级)都将触发此事件处理程序。
我可能只会创建一个额外的函数,将工具提示数据与类一起分配给每个元素。
$.fn.extend({
tooltip:function(text) {
text = text || '';
return $(this).each(function() {
$(this).data('tooltip',text).addClass('tooltip');
});
}
});
$('#someID').tooltip("Click me!");
$('button').tooltip("I'm a button");
https://stackoverflow.com/questions/10694557
复制相似问题