最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery ToolTip Not Working After AJAX call - Stack Overflow

programmeradmin1浏览0评论

I'm having a problem similar to this: JavaScript not working inside AJAX loaded DIV but my problem is that I don't have any event to bind to. I have this:

$('[title]').colorTip({color:'yellow'});

binding all elements with 'title' attribute to that object. It works fine on page reload. But on elements from AJAX call the story is different, it displays like the javascript doesn't exist. I know to bind elements to other events from AJAX using live(), but how do I bind elements that don't have 'events'?

I'm having a problem similar to this: JavaScript not working inside AJAX loaded DIV but my problem is that I don't have any event to bind to. I have this:

$('[title]').colorTip({color:'yellow'});

binding all elements with 'title' attribute to that object. It works fine on page reload. But on elements from AJAX call the story is different, it displays like the javascript doesn't exist. I know to bind elements to other events from AJAX using live(), but how do I bind elements that don't have 'events'?

Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Jan 31, 2013 at 18:09 IROEGBUIROEGBU 94816 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

you have to re-bind the tooltip after the ajax call.

$( document ).ajaxStop( function() {
    $('[title]').colorTip({color:'yellow'});
});

Or as suggested by jbabey, you might use the plete callback to re-bind the tooltip (simplified version from here):

$(
function(){
    // Get a reference to the content div (into which we will load content).
    var jContent = $( "#content" );             
    // Hook up link click events to load content.
    $( "a" ).click(
        function( objEvent ){
            var jLink = $( this );
            // Clear status list.
            $( "#ajax-status" ).empty();
            // Launch AJAX request.
            $.ajax(
                {
                    // The link we are accessing.
                    url: jLink.attr( "href" ),
                    // The type of request.
                    type: "get",
                    // The type of data that is getting returned.
                    dataType: "html",
                    plete: function(){
                        console.log("finalized ajax request");
                        //re-bind the tooltip
                        $('[title]').colorTip({color:'yellow'});
                    },
                    success: function( strData ){
                        console.log("ajax success");
                        console.log(strData);
                        // to something with the received data
                        jContent.html( strData );
                    }
                }                           
                );
            // Prevent default click.
            return( false );                    
        }
        );

}
);

Based upon your ment I included the following: You also have to make sure you bind the tooltip on page load:

$( document ).ready( function() {
    $('[title]').colorTip({color:'yellow'});
});

You can create a change event on the element.

$('title').live('change', function () {
whatever you want to do here
})

Then you would just trigger the change whenever you want the attribute to take effect.

$('title').trigger('change');
发布评论

评论列表(0)

  1. 暂无评论