Javascript 为什么不是';更新页面上的HTML是否正确?

Javascript 为什么不是';更新页面上的HTML是否正确?,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,网站:(注册帐户选项卡) 我正在创建一个AJAX处理程序,这样我就可以为我的网站创建一个AJAX JavaScript文件,然后通过它传递所有AJAX调用 到目前为止一切正常,它可以将AJAX请求发送到正确的文件并以JSON格式接收数据,但是在更新页面上的HTML时,我遇到了一个问题 我正在传回一个变量,该变量具有要放置HTML的对象的ID 我还用HTML传回另一个变量以放入该对象中。我很好地从AJAX调用中恢复了所有数据。但是,它根本不会更新页面上的HTML 这是我的代码: /** * $v

网站:(注册帐户选项卡)

我正在创建一个AJAX处理程序,这样我就可以为我的网站创建一个AJAX JavaScript文件,然后通过它传递所有AJAX调用

到目前为止一切正常,它可以将AJAX请求发送到正确的文件并以JSON格式接收数据,但是在更新页面上的HTML时,我遇到了一个问题

我正在传回一个变量,该变量具有要放置HTML的对象的
ID

我还用HTML传回另一个变量以放入该对象中。我很好地从AJAX调用中恢复了所有数据。但是,它根本不会更新页面上的HTML

这是我的代码:

/**
 * $value contains an array of the following
 *
 * content      The HTML code that is replacing the content of the specified div.
 * fade         Whether to fade out the ID or not before changing the content.
 * fadeOut      Whether to fade out the div after a certain amount of time.
 * function     Function to call at the end of the case.
 * restore      Restore the original content in the div. Only used if fadeOut is set.
 * targetId     The ID of the target div that is having it's content replaced.
 */                  
case 'html':
    console.log(value);
    var $target = $('#' + value.targetId);
    var original = '';
    if(value.restore) original = $target.html();
    if($target.length > 0) {
        if(value.fade) {
            $target.fadeOut(500);
            setTimeout(function() {
                $target.html(value.content).fadeIn(500);
            }, 510)
        } else {
            $target.html(value.content);
        }
    }
    if(value.fadeOut > 0) {
        setTimeout(function() {
            $target.fadeOut(500);
        }, value.fadeOut);
    }
    if(value.fadeOut > 0 && value.restore) {
        $target.html(original);
    }
    if(value.function != false) {
        window[value.function]();
    }
    break;
console.log(value)
生成预期的值,它给我正确的
ID
和HTML,如果我这样做了
console.log($target)
,那么我将返回长度为1的jQuery对象

fadeOut
工作正常,所以我知道我可以很好地操作
$target
那么为什么我不能替换HTML呢

我已经创建了一个新的应用程序,它很有效

那么为什么它在我的AJAX中不起作用呢


更新

在@nrabinowitz注释之后,我删除了所有代码,除了更新HTML之外,它还起了作用。此代码适用于:

case 'html':
    console.log(value);
    var $target = $('#' + value.targetId);
    $target.html(value.content);
    break;
什么可能导致它在if-else语句中停止工作


更新2


在将案例拆开并重新组合(使用撤消、不是复制粘贴或其他操作,只是撤消删除)之后,它就可以工作了。代码与我最初发布的代码相同,并且它突然起作用了><。。。仔细想想。谢谢大家的帮助。

控制台中是否有错误?是否尝试将
console.log()
添加到超时处理程序中?如果小提琴使用此代码,则此代码不是问题所在,并且您查找的位置不正确。根据我的经验,这类事情通常涉及一些你没有完全处理的异步过程。如果
value.fade
false
value。fadeOut
是非零和
value。restore
true
那么你的代码在更新为新值后会将内容重置回原始值。它会可能是某种缓存问题。确保在开发时打开浏览器缓存,这将为您省去很多麻烦。