Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery/Ajax按ID更新Span或Div而不刷新页面_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript Jquery/Ajax按ID更新Span或Div而不刷新页面

Javascript Jquery/Ajax按ID更新Span或Div而不刷新页面,javascript,jquery,ajax,Javascript,Jquery,Ajax,因此,我有以下代码: <span class='numberofthings' id='123456'> Things: ".$things."</span> 我见过使用以下方法更新div的方法: $("#divId").load(location.href + " #divId>*", ""); 但我试着把它应用到我的处境中,但没有成功。基本上,我需要更新span的原因是因为我有其他代码更改$things的值您应该从成功回调中获得新的值,并使用.html()更

因此,我有以下代码:

<span class='numberofthings' id='123456'> Things: ".$things."</span>
我见过使用以下方法更新div的方法:

$("#divId").load(location.href + " #divId>*", "");

但我试着把它应用到我的处境中,但没有成功。基本上,我需要更新span的原因是因为我有其他代码更改$things的值

您应该从
成功
回调中获得新的值,并使用
.html()更新


让其使用ID重新加载上面的Span
这是什么意思?是否要更新Span文本?是。。或者HTML也可以使用“重新加载”一词,这会让很多人感到困惑,更好的术语是“更新”好的,谢谢。另外,假设我更新html所需的ID不是#123456,而是一个变量,比如变量名为“thingsvariable”。我怎样才能解决这个问题,让它正常工作?var thingsvariable=“”#例如“$html(“事物:+新事物”);对注释中的代码将满足您的要求。但事实并非如此,如果您编写
$(“#例如”).html(“Things:+new#Things”),我只是仔细检查代码是否有效
而不是
var thingsvariable=“”#例如“$html(“事物:+新事物”)?好的,谢谢你花时间回复并帮助我。我做的是“foranample”,而不是“foranample”。。不过现在已经整理好了,再次谢谢
$("#divId").load(location.href + " #divId>*", "");
function click(obj) {    
    $.ajax({
        ...
        success: function() {
            var new_things = ''; // Here is where you fetch new data from the response.
            $('#123456').html("Things: " + new_things);
        }
    });
}