xml是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tutorial xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- ************************************** DAY 1 MODULE 1 **************************** -->
<qn day="1" module="1" id="1" qntxt="Day 1 Mod 1 First qn?" ans="Answer 3" desc="The Correct answer is Answer3 because...">
<opt id="Answer 1"></opt>
<opt id="Answer 2"></opt>
<opt id="Answer 3"></opt>
<opt id="Answer 4"></opt>
</qn>
<qn day="1" module="1" id="2" qntxt="Day 1 Mod 1 Second qn?" ans="Answer 22" desc="The Correct answer is Answer1 because...">
<opt id="Answer 21"></opt>
<opt id="Answer 22"></opt>
<opt id="Answer 23"></opt>
<opt id="Answer 24"></opt>
</qn>
<qn day="1" module="1" id="3" qntxt="Day 1 Mod 1 Third qn?" ans="Answer 34" desc="The Correct answer is Answer4 because...">
<opt id="Answer 31"></opt>
<opt id="Answer 32"></opt>
<opt id="Answer 33"></opt>
<opt id="Answer 34"></opt>
</qn>
我正在解析并将DayID,ModuleID插入数据库。这工作正常。 除此之外,我还插入了“问题总数” - 这是每个qn节点下'opt'子节点的计数。我怎么能得到这个?
function xmlParser(data) {
dropTable();
createTable();
xml = data;
var dayID, moduleID;
$(xml).find('day').each(function()
{
**alert($(this).children.length);**
dayID = $(this).attr('id');
$(this).find("module").each(function()
{
moduleID = $(this).attr('id');
DayID = dayID;
ModuleID = moduleID;
//CurScore = 0;
//HighScore = 0;
//alert(dayID +'--' + moduleID);
insertRecord(dayID,moduleID,0,0,0);
});
});
}
我用 - $(this).children.length; - 但它给了我错误的价值 - 我得到2 - 实际值是4。
如何在不进一步循环/更改代码的情况下获得此功能?
答案 0 :(得分:0)
试试这个: - http://jsfiddle.net/adiioo7/sPRgR/
HTML: -
<div id="test">
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tutorial xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- ************************************** DAY 1 MODULE 1 **************************** -->
<qn day="1" module="1" id="1" qntxt="Day 1 Mod 1 First qn?" ans="Answer 3" desc="The Correct answer is Answer3 because...">
<opt id="Answer 1"></opt>
<opt id="Answer 2"></opt>
<opt id="Answer 3"></opt>
<opt id="Answer 4"></opt>
</qn>
<qn day="1" module="1" id="2" qntxt="Day 1 Mod 1 Second qn?" ans="Answer 22" desc="The Correct answer is Answer1 because...">
<opt id="Answer 21"></opt>
<opt id="Answer 22"></opt>
<opt id="Answer 23"></opt>
<opt id="Answer 24"></opt>
</qn>
<qn day="1" module="1" id="3" qntxt="Day 1 Mod 1 Third qn?" ans="Answer 34" desc="The Correct answer is Answer4 because...">
<opt id="Answer 31"></opt>
<opt id="Answer 32"></opt>
<opt id="Answer 33"></opt>
<opt id="Answer 34"></opt>
</qn>
</div>
JS: -
$(function(){
var xml = $("#test").html();
var xmlDoc = $.parseXML( xml );
$(xmlDoc).find("qn").each(function(){
alert($(this).find("opt").length);
});
});
答案 1 :(得分:0)
如果您正在寻找特定标记,在我们的案例opt
中,您应该将其作为选择传递给.children()
,因为孩子会接受标记的所有子项。然而,在你的情况下,你正在取一个函数对象的长度(children
是一个函数对象,children('opt')
或children()
是对所述函数的调用)
$(xmlDoc).find("qn").each(function(){
numberOfQuestions = $(this).children("opt").length;
});
请注意,在您使用.find()
的特定情况下,您将获得完全相同的数字。然而,两者之间存在差异。 find
将遍历从选择到树的底部的DOM。也就是说,如果您的XML更改为
<qn day="1" module="1" id="1" qntxt="Day 1 Mod 1 First qn?" ans="Answer 3" desc="The Correct answer is Answer3 because...">
<opt id="Answer 1">
<qn day="1" module="1" id="1" qntxt="Day 1 Mod 1 First qn?" ans="Answer 3" desc="The Correct answer is Answer3 because...">
<opt id="Answer 1"></opt>
<opt id="Answer 2"></opt>
<opt id="Answer 3"></opt>
<opt id="Answer 4"></opt>
</qn>
</opt>
<opt id="Answer 2"></opt>
<opt id="Answer 3"></opt>
<opt id="Answer 4"></opt>
</qn>
然后使用find('opt')
您将选择8个元素,而使用children('opt')
只能获得4个。有关.children(\[selector\])