如何使工具提示与ajax一起使用

时间:2012-11-14 06:40:36

标签: javascript ajax jquery

Q1)我正在使用来自twitter bootstrap的tootltip。我刚注意到当内容添加了ajax时它无法正常工作。经过大量的谷歌搜索,解决方案似乎在ajax请求后触发工具提示。但在我的情况下,这是不可能的,因为我依赖于框架内置的ajax API。还有其他工作吗?

$('.tootip').tooltip({placement:'left'});

Q2)在jQuery on()文档中,用法被称为

$(document).on(event, selector, function(){ //do stuff here })

我必须这样做吗?

$(document).on('ready', '.tootip', tooltip({placement:'left'}));

但它不起作用。

1 个答案:

答案 0 :(得分:1)

A1)你给ajax调用的一个选项/参数是一个回调函数,它在ajax调用完成并成功时触发。此成功回调应初始化工具提示 例如,如果您使用的是jQuery:

$.ajax({
    url: 'your url'
    success: function(result) {
        // do your sruff here. Result holds the return data of the ajax call
    }
});

A2)查看第3个参数:function(){ //do stuff here }。你必须提供一个功能。相反,你提供的是调用函数tooltip({placement:'left'})的结果,在这种情况下函数返回一个对象而不是一个函数。你应该这样做:

$(document).on('ready', '.tootip', function() {
    $('.tootip').tooltip({placement:'left'}); 
});

有关您的评论的更新:
在函数内部,无论是成功回调还是事件函数,您都可以做任何您喜欢的事情,包括调用多个函数:

$(document).on('ready', '.tootip', function() {
    // Do many operations as much as you like here
    func1();
    func2();
});

$.ajax({
    url: 'your url'
    success: function(result) {
        // Do many operations as much as you like here
        func1();
        func2();
    }
});

希望这有帮助!