Skip to main content
 首页 » 编程设计

jquery之如何重新加载/重绘/更新 jQuery 插件来刷新其值

2026年07月28日5小虾米

我相信这很容易实现,但我找不到如何做到这一点。是否可以在不刷新整个页面的情况下重新加载 jQuery 插件,以便它可以刷新其值?我读过类似的问题,但它们针对特定的插件。我想知道是否有人知道实现此目的的“通用”方法,以便它可以在许多 JQ 插件中使用。

无论如何,更具体地说,我问这个问题是为了重新加载 jQuery File Tree 。我有这个:

JavaScript

$(document).ready( function() { 
    $('#fileTree').fileTree({ 
        root: '/some/path/to/somewhere/', 
        script: 'js/jqueryFileTree-1.01/connectors/jqueryFileTree.php', 
        folderEvent: 'click', 
        expandSpeed: 1000, 
        collapseSpeed: 1000, 
        multiFolder: false, 
        loadMessage: 'Loading...' 
        }, function(file) { 
            relativePath=file.replace("/some/path",""); 
            aFunction(relativePath, file); 
        }); 
    }); 

HTML

<div id="fileTree" class="fileTree"></div> 

在某个时刻,我有 AJAX+PHP 代码,可以让我删除所选文件。这很好用!但已删除的文件仍然显示在文件树中。如果我在该函数的成功事件中添加 window.location.reload() ,显然整个页面都会重新加载,并且我的树会按预期显示文件夹中的实际文件。

问题:有没有办法在不刷新整个页面的情况下重新加载它?

请注意这个插件的作者在 Twitter 上告诉我:

@metafaniel Problem is it's no longer used in any of our projects. It's an old, old script that needs an overhaul. via @abeautifulsite

@metafaniel You'll have to add that yourself. As the plugin is very old, it doesn't have a complete API. via @abeautifulsite

我没有更多的时间用另一个插件重写我的代码,我必须这样完成它。

请您参考如下方法:

好吧,如果OP要求直接插件命令来执行此操作,恐怕它可能不存在。这是我的解决方案,效果很好。你所做的就是包裹你的空<div id="jstree"></div>在父 div 中,如下所示:

<div id="file-tree-holder"> 
    <div id="jstree" ></div> 
</div> 

然后,在您的 JavaScript 中,在实例化插件之前,清除父 div 的内容,然后在父 div 中重写目标 div,如下所示:

        $('#file-tree-holder').html(''); 
        $('#file-tree-holder').html('<div id="jstree" ></div>'); 
        $('#jstree').fileTree({ 
            script: '/ajax/file_tree2/' + path, 
            multiFolder: true, 
            expandSpeed: 250, 
            collapseSpeed: 250, 
            loadMessage: 'Loading...' 
        }); 

...而且效果非常好。