2011-09-15 27 views
0

我使用ajax加载页面(example.html)。我有两个按钮:一个用于ajax加载功能,另一个用于加载内容。但它没有反应。我试图用:jquery如何选择和使用ajax加载元素

$(document).ready(function(){ 
    $("#load") 
     .click(function(){ 
     $("content").load("example.html"); 
    }); 
     $("#example_content").load(function(){ 
        // some actions to loaded page 
}); 

回答

3

jQuery的负载功能并不像和事件钩子函数的工作,所以第二.load通话将希望收到类似于URL字符串,使服务器的新请求得到更多数据(链接http://api.jquery.com/load/)。

如果你想要做的事与已加载到div的内容,我建议你使用AJAX方法,它可以像这样使用:

$.ajax({ 

    //where is the data comming form? url 
    url : "example.html", 

    //what happens when the load was completed? the success function is called 
    success : function(data){ 
     //add the content from the html to the div 
     $("content").html(data.responseText); 

     //do whatever else I want with the content that was just added to 'content' 
     console.debug($('content')); // should show you all the html 
            // content written into that element 

     //my content specific funciton 
     myFunction(); 
    } 
}); 

如果你想更短的方式,使用$ .get(url,success)函数可以帮助你,但是这里面使用了$ .ajax,所以你最好直接使用$ .ajax。

回顾:

1).load是使用。获得抓取内容的功能。 http://api.jquery.com/load/

2).get有一个成功函数,它将从给定url接收的内容写入目标元素。 http://api.jquery.com/jQuery.get/

3).get是一个函数.ajax,它是jQuery ajax功能的核心。 http://api.jquery.com/jQuery.ajax/

+0

感谢您的详细回复 – djayii

相关问题