刷新没有id的div的内容

时间:2014-02-13 19:23:24

标签: javascript jquery database-connection

我正在尝试向表中添加刷新按钮。我目前正在使用。

var $target = $(this).parent().parent().next('.box');
$target.load("$site #" +$target.prop("nodeName");

页面上有很多.box。当我使用.load()时,我可以通过DOM中的位置引用特定的.box吗?

另外,有没有办法延迟表加载告诉数据库中的更新已经完成?我在加载内容之前处理数据库的更新,但它似乎仍然加载,好像数据库更新没有发生,并且需要刷新。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以稍微重新组织Html,例如将.box内容包装在.refresh div中

<section class="col col-lg-8">
    <div class="box">
        <div class="box-header">
                <h2> <i class="fa fa-user"></i><span class="break"></span>Recently Added</h2>

            <div class="box-icon"><a class="btn-minimize" href="#"><i class="fa fa-chevron-up"></i></a>

            </div>

        </div>
        <!-- Box Header -->
        <div class="box-icon"><a class="btn-reload" href="#">reload<i class="fa fa-circle"></i></a>


        <div class="box-content">
            <table class="table table-striped table-bordered bootstrap-datatable datatable">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Date registered</th>
                        <th>Role</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
            </table>
        </div>
   </div>

然后var $target = $(this).parent().parent().next('.box-content');可以更改为:

var $target = $(this).find('.box-content');

否则,只要HTML维护每个部分的布局,您现在应该使用.parent.parent样式;您正在使用parent.parent.next代码在DOM中引用它的位置,因为您知道这将是正确的box-content div的位置。

编辑:详细说明为什么find适用于您的html结构.find和类似的.children元素只查看后代元素:来自http://api.jquery.com/find/

基于示例Html,您向我展示了:

<.box>
    <box descendant>
        <descendant element>
    </box descendant>
    <box descendant2>
    </box descendant2>
</box>
<.box>
    <box descendant>
        <descendant element>
    </box descendant>
    <box descendant2>
    </box descendant2>
</box>

这样做的:

 $(.box).click(function(){
    $(this).find(descendant element)
});

将始终获得正确的元素,因为.find将保留在父元素