简体   繁体   English

单击将动态外部内容加载到另一个的Div中

[英]Loading dynamic external content into the Div of another on click

Trying to load an external php file into another onClick. 尝试将外部php文件加载到另一个onClick中。 Iframes will not work as the size of the content changes with collapsible panels. 由于可折叠面板的内容大小会发生变化,因此iframe无法使用。 That leaves AJAX. 剩下的就是AJAX。 I was given a piece of code 我得到了一段代码

HTML 的HTML

<a href="#" id="getData">Get data</a>
<div id="myContainer"></div>
JS

$('#getData').click(function(){
    $.get('data.php', { section: 'mySection' }, function(data){
       $('#myContainer').html(data);
    });
});
PHP:

<?php
    if($_GET['section'] === 'mySection') echo '<span style="font-weigth:bold;">Hello World</span>';
?>

I have tested it here http://www.divethegap.com/scuba-diving-programmes-dive-the-gap/programme-pages/dahab-divemaster/divemaster-trainingA.php and get the most unexpected results. 我在这里http://www.divethegap.com/scuba-diving-programmes-dive-the-gap/programme-pages/dahab-divemaster/divemaster-trainingA.php中进行了测试,并获得了最意外的结果。 It certainly loads the right amount of items as it says in the lower bar on safari but I see three small calendars and that is it. 它肯定会加载正确数量的项目,如在Safari的下部栏中所述,但我看到了三个小日历,仅此而已。 Can anyone see where I have made a mistake? 谁能看到我在哪里弄错了?

First things first, you want to have the jQuery bit inside the ready function $(document).ready(function(){..} . In your test page, there is already a ready function at the top which contains the line $('a[rel*=facebox]').facebox() , so you might want to put the code there. 首先,您需要在ready函数$(document).ready(function(){..}包含jQuery位。在您的测试页中,顶部已经有一个ready函数,其中包含$('a[rel*=facebox]').facebox() ,因此您可能希望将代码放在此处。

Secondly, you want to prevent the link from going to the default action, which is to load the url '#'. 其次,您要防止链接转到默认操作,即加载URL'#'。 You do that with the preventDefault() method. 您可以使用preventDefault()方法来实现。

I tested and confirmed that the following code should work for you: 我测试并确认以下代码适用于您:

<script type="text/javascript">
  $(document).ready(function(){
    $('#getData').click(function(event){
      event.preventDefault();
      $.get('test.php', { cat: 16 }, function(data){
        $('#myContainer p').html(data);
      });
    });
});</script>

You can find more details and examples of jQuery's get function here . 您可以在此处找到jQuery的get函数的更多详细信息和示例。

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

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