简体   繁体   English

页面加载后如何加载特定DIV中的内容?

[英]How do I load content in specific DIV after the page loads?

I am wondering how I would load another PHP page into a DIV container after the parent page has loaded. 我想知道在父页面加载后如何将另一个PHP页面加载到DIV容器中。 I need to design facebook/twitter share links that will show people my page with certain content loaded into a DIV. 我需要设计Facebook / Twitter分享链接,以向人们展示我的页面,其中某些内容已加载到DIV中。

I have a function working for clicking links, but I need it to work on page load rather than click (#results is the ID of the DIV I need content loaded into): 我有一个用于单击链接的函数,但是我需要它在页面加载而不是单击时起作用(#results是需要加载内容的DIV的ID):

$(".Display a").click(function() {
  $.ajax({
   url: $(this).attr("href"),
   success: function(msg){
     $("#results").html(msg);
   }
 });
 return false;
});

You can use jQuery's .ready() event on the document: 您可以在文档上使用jQuery的.ready()事件:

$(document).ready(function () {
    // Whatever you want to run
});

This will run as soon as the DOM is ready. DOM准备就绪后,它将立即运行。

If you need your javascript to run after everything is loaded (including images) than use the .load() event instead: 如果您需要在所有内容(包括图像)加载后运行javascript,则可以使用.load()事件代替:

$(window).load(function () {
    // Whatever you want to run
});

I'd suggest keeping your original click-handler, and triggering it with: 我建议您保留原始的点击处理程序,并通过以下方式触发它:

$(".Display a").click(function() {
  $.ajax({
   url: $(this).attr("href"),
   success: function(msg){
     $("#results").html(msg);
   }
 });
 return false;
});

$(document).ready(
    function(){
        $('.Display a').trigger('click');
    });

Have you tried just using the $.ajax() outside of the click event? 您是否尝试过在click事件之外使用$ .ajax()?

Instead of -- 代替 -

$(".Display a").click(function() {
    $.ajax({
      url: $(this).attr("href"),
      success: function(msg){
        $("#results").html(msg);
      }
    });
  return false;
});

Try this -- 尝试这个 -

$(document).ready(function () {
      $.ajax({
        url: $(this).attr("href"),
        success: function(msg){
                 $("#results").html(msg);
        }
       }); 
});
$(function(){ //jQuery dom ready event

    $(".Display a").click(function() {
        ///you code
    }).click(); //trigger it at the page load

});

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

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