简体   繁体   English

使用.load和jQuery在DIV中加载HTML页面

[英]Load HTML page inside a DIV using .load and jQuery

I have a function in PHP that renders and return a HTML content and I need to load that result on a DIV when page load. 我在PHP中有一个函数可以呈现并返回HTML内容,并且在页面加载时需要在DIV上加载结果。 I'm doing in this way: 我是这样做的:

 <div id="myModal" class="reveal-modal" title="El Farol" data-animation="fade"> <a class="close-reveal-modal">&#215;</a>
     <script>
         $(document).ready(function() {
             $(this).load("http://devserver/app_dev.php/register");
         });
     </script>
 </div>

But I get nothing, if I call http://devserver/app_dev.php/register from browser I get the HTML. 但是我什么也没得到,如果我从浏览器中调用http://devserver/app_dev.php/register ,我会得到HTML。 What I'm doing wrong? 我做错了什么? What is the right way to load HTML content in a container on DOM complete load or page load? 在DOM完全加载或页面加载时加载容器中的HTML内容的正确方法是什么?

Try this: 尝试这个:

I inserted the link the a href tag instead of the script... It will be easier to replace links that way. 我在链接中插入了a href标签而不是脚本...用这种方式替换链接会更容易。

HTML: HTML:

<div id="myModal" class="reveal-modal" title="El Farol" data-animation="fade">
<a class="close-reveal-modal" href="http://devserver/app_dev.php/register">Page 1</a>

JQuery: jQuery的:

$(document).ready(function(){
  $('a').click(function(e){
     e.preventDefault();
     $("#myModal").load($(this).attr('href'));
  });
});

Your html and JS should be separated like this: 您的html和JS应该像这样分开:

<div id="myModal" class="reveal-modal" title="El Farol" data-animation="fade">
</div>

 <script>
     $(document).ready(function() {
         $('#myModal').load("/app_dev.php/register", function() {
              $('#myModal').append( '<a class="close-reveal-modal">&#215;</a>' );
         });
     });
 </script>

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

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