我目前有以下内容:
HTML:
<div class="content-class"></div>
JQuery Ajax:
$(document).ready(function() {
$(document).on("click", ".content-class", function(event) {
event.preventDefault();
var post = $(this);
$.ajax({
url: 'script.php',
type: 'POST',
data: { /* some data */ },
success: function(data) {
// load content from php file into .content-class
});
});
});
如何更改该功能,以便将PHP文件中的内容加载到.content-class而不必单击它(以便它在页面加载时执行)?
感谢。
答案 0 :(得分:3)
只需删除click事件侦听器并立即执行ajax:
$(document).ready(function() {
$.ajax({
url: 'script.php',
type: 'POST',
data: { /* some data */ },
success: function(data) {
// load content from php file into .content-class
$('.content-class').html(data);
});
});
答案 1 :(得分:1)
正如PSL所说,只需将click事件处理程序之外的ajax调用移动:
$(document).ready(function() {
$.ajax({
url: 'script.php',
type: 'POST',
data: { /* some data */ },
success: function(data) {
// load content from php file into .content-class
});
});