简体   繁体   English

单击时使用Jquery刷新div

[英]Refresh div with Jquery on click

Lets say I have a page with div and mysql data in it. 可以说我有一个包含div和mysql数据的页面。 For example code below: 例如下面的代码:

<div id="example">
  <table class="table table-striped table-bordered">
    <thead>
      <tr>
        <th>#</th>
        <th>Name</th>
      </tr>
    </thead>
    <tbody>

      <?php $num_rows=1 ; // store the record of the "tblstudent" table into $row while ($row=m ysqli_fetch_array($result)) { // Print out the contents of the entry echo '<tr>'; echo '<td>' . $num_rows . '</td>'; echo '<td>' . $row[ 'name'] . '</td>'; $num_rows++;
      } ?>

    </tbody>
  </table>
</div>

If I want to only refresh certain part of my page in this case the <div id="example"> , How can I do that with just a single page and not using .load() or $.post . 如果在这种情况下,我只想刷新页面的某些部分<div id="example"> ,我该如何只用一个页面而不使用.load()$.post做到这一点。 Is it actually possible?? 实际上有可能吗?

What you posted is PHP - within PHP there's no way to update the page at all. 您发布的是PHP-在PHP中根本无法更新页面。 You'd be using JavaScript do that. 您可能会使用JavaScript来做到这一点。

If you want to refresh a certain part of your page, AJAX is typically the way to do it (either $.ajax or .load() ). 如果要刷新页面的特定部分,通常可以使用AJAX来完成( $.ajax.load() )。 Since you don't want/are unable to use AJAX, an IFRAME is really the only other way to do it. 由于您不希望/无法使用AJAX,因此IFRAME实际上是唯一的其他方法。 Frame out your content in a separate page, then include it via an IFRAME. 在单独的页面中将内容框架化,然后通过IFRAME将其包括在内。 You can use one of these two methods to refresh it: 您可以使用以下两种方法之一来刷新它:

document.getElementById('#FRAME').contentDocument.location.reload(true);

var iframe = document.getElementById(FrameId);
iframe.src = iframe.src;

what you do clone your object on page load. 您在页面加载时所做的克隆对象。 You need to take global variable. 您需要采用全局变量。

var cloneObj;
$(document).ready(function (){
    cloneObj= $("#example1").clone();
});

Now whenever you need to refresh that element just call this block of code given below. 现在,每当您需要刷新该元素时,只需调用以下代码即可。

$("#example1").replaceWith(cloneObj.clone());

I have used this code in my previous answer. 我在以前的答案中使用了此代码。 please check this FIDDLE 请检查此FIDDLE

I hope it helps you. 希望对您有帮助。 If not than please ask. 如果不是,请询问。 I will try again 我会再试一次

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

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