我有一个带下拉菜单的bootstrap导航头。我试图用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>
jQuery
$( window ).load(function() {
//get user location details
$.get("http://ipinfo.io", function (response) {
var userCity = response.city;
var userRegion = response.region;
//append to header
$('#userRegion').html(userRegion);//does not work
$('#userCity').html(userCity);//does not work
//$('#alist2345').html(userRegion + userCity);//EDIT: THIS IS JUST AN EXAMPLE to show that this one works but it is obviously not the desire outcome
}, "jsonp");
});
发布于 2018-08-17 23:46:14
感谢大家的努力!
我发现了错误,这不是我的编程能力(天)。我们基本上在这个项目中有几个人,其中一个程序员在桌面上上传了一个重复的文件,奇怪的是,应用程序从该文件中提取数据,而不是从应用程序文件夹中的文件中提取数据...??
在控制台中的更深层次的观察证明这是真的。我可以看到前一个文件中的旧数据,而不是我正在处理的文件。
非常奇怪的行为,但当我删除了我的同事之前上传测试的重复文件(并忘记删除),一切都开始正常工作。
非常奇怪的行为,但现在一切都好了。
Ps:如果你知道为什么会发生这种情况,请让我知道,因为我想知道更多关于这个主题的信息!
发布于 2018-08-17 02:56:56
它确实如预期的那样将html
注入到跨度中,但是最后一次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);
发布于 2018-08-17 03:07:11
那是因为你在写
$('#userRegion').html(userRegion);//does not work
$('#userCity').html(userCity);//does not work
然后用来清除整个锚点元素
$('#alist2345').html(userRegion + userCity);//works but not the desire outcome
https://stackoverflow.com/questions/51883576
复制相似问题