我有一个JQ函数,它检索一些结果,如果成功,它应该列出一个列表并将其添加到一个元素中。我根据结果创建了一个UL -以下是成功函数:
success: function (info) {
var shipActionsUL = '<ul class="shipActionsList>';
$(info).find('string').each(function (index) {
var action = $(this).text();
shipActionsUL += '<li id="'+action+'">'+action+'</li>';
});
shipActionsUL += '</ul>';
$('#hello').text(shipActionsUL);
}现在,当#hello的文本发生变化时,结果是:
<ul class="shipActionsList>
<li id="aaMove">aaMove</li>
<li id="aaAttack">aaAttack</li>
</ul>这就是我希望UL在DOM中看起来的样子。然而,这是文字文本。当我尝试使用$(‘#hello’).html(ShipActionsUL)来创建列表时,我得到了一个奇怪的UL --它的第一个列表项漏掉了它的子弹,当我检查列表时,它看起来是这样的:
<ul class="shipActionsList>
<li id=" aamove">aaMove</li>
<li id="aaAttack">aaAttack</li>
</ul>注意一个爱-小写的id和它前面的额外空间。不管列表中的项目是什么,它总是第一个被弄乱的元素。
另外,如果我要从只有
shipActionsUL += '<li>'+action+'</li>';在$(‘#hello’).html(ShipActionsUL)上,列表根本不会出现在#hello中;
-来自成功的信息--将采用这种格式(现在只有aaAttack会被搞砸):
<?xml version="1.0" encoding="utf-8"?>
<ShipActions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<shipID>162</shipID>
<Actions>
<string>aaAttack</string>
<string>aaMove</string>
<string>aaSttack</string>
</Actions>
</ShipActions>发布于 2013-08-04 17:20:56
我认为这是因为你在UL标签上遗漏了一个结尾的引号:
var shipActionsUL = '<ul class="shipActionsList">'; //Previously missing closing double quote.此外,如果不希望ID包含额外的空间,则可以修剪action
var action = $(this).text().trim();发布于 2013-08-04 17:22:34
非常奇怪的是,在第一个列表项的文本节点之前没有空格。如果你这样做了呢
var action = $.trim($(this).text());https://stackoverflow.com/questions/18045453
复制相似问题