将PHP加载到DIV但包含URL

时间:2017-03-03 18:05:05

标签: php jquery html

我在另一个帖子中找到了我的问题:How to load PHP file into DIV by jQuery?

我唯一的问题是:我无法向某人发送一个链接,其中包含加载到did中的某个文件。它总是只是主页。有没有办法将其纳入网址?

我使用了以下代码:

   $(document).ready(function(){
$("#artists").click(function(){
$("#content").load('urltocontent.php');
  });
 });

在我的index.php中我有一个#content,一切顺利。

我计划在该div中打开php中的每个内容。但是当我打开页面时,div当然是空的,因为没有单击链接来打开div中的内容。有没有办法生成一个打开内容的网址?所以我可以传播链接

2 个答案:

答案 0 :(得分:0)

假设您正在调用此页面:http://example.com?url=page_to_include.com

您可以使用此代码:

$(document).ready(function(){
      $("#contents").load('<?php echo $_GET['url']; ?>');
});

答案 1 :(得分:0)

你可以使用#..这就是它通常在javascript中完成的方式。所以在网址后面添加#artists。例如:

$(function(){
  $("#button1").click(function(){
      $("#content").html("content of button 1. can load with ajax like you did");
  });

  $("#button2").click(function(){
      $("#content").html("content of button 2. can load with ajax like you did");
  });

  $(document.URL.substr(document.URL.indexOf('#')) ).click();


})

现在转到yoururl.php#button1