Javascript html()似乎不适用于li中的span元素

Javascript html()似乎不适用于li中的span元素,javascript,jquery,Javascript,Jquery,我有一个带下拉菜单的引导导航标题。我试图用html()在其中一个下拉列表中添加一个单词,但没有成功。我尝试了append(),但它也不起作用。我不明白,因为列表是静态的,而不是动态的 下拉列表 <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#"> <i class="fa fa-bell fa-lg header-icon"

我有一个带下拉菜单的引导导航标题。我试图用html()在其中一个下拉列表中添加一个单词,但没有成功。我尝试了append(),但它也不起作用。我不明白,因为列表是静态的,而不是动态的

下拉列表

<li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
        <i class="fa fa-bell fa-lg header-icon" aria-hidden="true"></i>
         .'<span class="header-li">'.$Notifications.'</span>
         <i class="fas fa-caret-down header-icon"></i>
        <ul class="dropdown-menu">
         <li><a class="" href="#">'.$Youhavenewnotifications.'</a></li>
         <hr>
          <li><a id="alist2345" href="#">
           Hi visitor from <span id="userRegion"></span>!
           We have many customers like you from <span id="userCity"></span>!
        </a></li>
        </ul>
      </li>

它确实按预期将
html
插入span,但最后的
html
插入会覆盖以前的更改

$('#userRegion').html(userRegion);
$('#userCity').html(userCity);
$('#alist2345').html(userRegion + userCity); -- > This overwrites the `html` again ( and the span tags get replaced )
删除最后一条语句,它将按预期显示

否则你也可以试试这个

$('#alist2345').html($('#alist2345').html() + userRegion + userCity);

因为你在写作

$('#userRegion').html(userRegion);//does not work
$('#userCity').html(userCity);//does not work
然后用清洁剂清理整个锚固元件

$('#alist2345').html(userRegion + userCity);//works but not the desire outcome
除去

$('#alist2345').html(userRegion + userCity);
并将.html()更改为.text(),因为.html更改了“html”,并且.text()在元素中插入了一个sting

$(窗口).on('load',函数(){
$.get(”https://ipinfo.io?token=yourtoken-“转到此处”,功能(响应){
var userCity=response.city;
var userRegion=response.region;
$('#userRegion').text(userRegion);
$('#userCity').text(userCity);
}“jsonp”)
})



谢谢大家的努力

我发现了错误,这不是我的编程能力(phew)。我们基本上是这个项目的几个负责人,其中一个程序员在桌面上上传了一个重复的文件,奇怪的是应用程序从该文件而不是应用程序文件夹中获取数据

通过对控制台的深入观察,证明了这一点。我可以看到以前文件中的旧数据,而不是我正在处理的那个

非常奇怪的行为,但当我删除了我同事之前上传用于测试的重复文件(忘记删除)时,一切都开始正常工作

非常奇怪的行为,但现在没事了


Ps:如果你知道为什么会发生这种情况,请让我知道,因为我想知道更多关于这个主题的信息

对于shiggles,请在get回调中尝试
console.log($('#userRegion').length)
console.log(userRegion)
.text()
方法是否也没有提供所需的输出?只是确认一下!这对我来说很好:@barmar也这么做了,@MarkCarpenterJr为什么会有不同?她能够从API得到响应,因为注释掉的代码可以工作。我知道。最后一条语句只是一个示例,表明当我在元素上追加时,它是有效的。我已经编辑了我的问题。
$('alist2345').html(userRegion+userCity)被注释掉了。你是对的。我用.html()测试死亡。它可以正常工作,但是.text()是最好的选择-您想插入字符串我同意这是最好的,但是除非地区或城市包含HTML标记,否则没有任何区别。@Sadie请不要生气。90%的问题都是由完全不称职的程序员提出的,他们的问题往往非常愚蠢。当代码看起来像你的时候,没有办法知道你不是他们中的一员。@Sadie当一个问题被发布时,前提将基于我们所看到的。因此,他解释了可能导致这一问题的原因。我无意冒犯你。
$('#alist2345').html(userRegion + userCity);