2012-03-20 110 views
6

有人能帮忙吗?jQuery/AJAX - 点击按钮时将内容加载到div中?

我想填补一个例如

<div id="contenthere"></div> 

带有来自外部文件的内容例如,

/includes/about-info.html 

当点击某个类的按钮时,例如,

<p class="classloader">Click Here</p> 

有没有人有一个快速的代码示例,他们可以告诉我实现这个目标?

+0

http://api.jquery.com/load / – Blazemonger 2012-03-20 13:17:37

回答

13

使用jQuery.clickjQuery.load

$(document).ready(function(){ 
    $('.classloader.').click(function(){ 
     $('#contenthere').load('/includes/about-info.html'); 
    }); 
}) 
2

你可以订阅.click()事件的段落,然后使用.load()函数来发送一个AJAX请求给定的url和注入结果与指定选择:

$(function() { 
    $('.classloader').click(function() { 
     $('#contenthere').load('/includes/about-info.html'); 
     return false; 
    }); 
}); 
0

尝试以下操作:

var myurl = 'myfileonthesamedomain.html'; 

$('button').click(function(){ 
    $('div.loadme').load(myurl); 
}); 
2

加载最新版本的jQuery:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 

jQuery代码:

<script language="javascript"> 
$(function(){ 
    $(".classloader").click(function(){ 
    $("#contenthere").load("/includes/about-info.html"); 
    }); 
}); 
</script> 
</script> 

HTML代码:

<p class="classloader">Click Here</p> 
<div id="contenthere"></div>