简体   繁体   English

在页面加载时在div中加载内容

[英]Load content in div on page load

I currently have the following: 我目前有以下内容:

HTML: HTML:

<div class="content-class"></div>

JQuery Ajax: 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
        });
    });
});

How do I change that function so that it loads the content from the PHP file into .content-class without having to click it (so that it does it on page load)? 如何更改该函数,以便无需单击即可将PHP文件中的内容加载到.content-class中(以便在页面加载时进行加载)?

Thanks. 谢谢。

Just remove the click event listener and do the ajax straight away: 只需删除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);
    });
});

as PSL said, just move the ajax call outside of click event handler: 如PSL所说,只需将ajax调用移到click事件处理程序之外:

$(document).ready(function() {
    $.ajax({
        url: 'script.php',
        type: 'POST',
        data: { /* some data */ },
        success: function(data) {
            // load content from php file into .content-class
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM