.data()无法更新Chrome检查器中显示的代码

.data() fails to update code showed in the Chrome inspector

本文关键字:显示 代码 检查 Chrome 更新 data      更新时间:2024-01-06

我正在调用我的服务器来重命名一个特定的文件,这是代码客户端:

function rename_file(filepath, filename, object){
    $("#loading").html('<i class="fa fa-circle-o-notch fa-spin"></i>');
    $.getJSON('queries.php', { 
        q: 'renameFile', 
        old_path: filepath, 
        new_name: filename
    }, function(data){
        if (data.message == "File renamed") {
            $("#loading").html(data.message);
            console.log(data.name);
            var mod_name = data.name;
            object.data('file', mod_name);
            console.log(object.data('file'));
        } else {
            $("#loading").html(data.message);
        }
    });
}

函数的调用方式如下:

rename_file(old_filepath, new_filename, $(this));

其中$(this)为:

<div class="filename_container" id="file_165161" data-file="toto.doc" data-folder="" class="fileClic">
    toto.doc
</div>

当我登录控制台时,当我将toto.doc重命名为toto2016.doc时,我得到的是:

toto2016.doc
toto2016.doc

所以data-file中包含的数据应该是toto2016.doc,但当我在Chrome检查器中检查时,我仍然得到toto.doc。因此,如果我重命名文件两次,它就会失败。

我已经测试了正确的对象被传输到函数,事实确实如此。我在这里做错了什么吗?

这是经过设计的。当您使用data()设置值时,它只更新jQuery保存在内存中的对象,而不更新DOM中元素的任何属性。

如果您使用data()的getter检索该值,您将看到该值已被正确设置。