好的,我有一个XML / XSLT配对(通过Javascript从2个外部文件插入我的HTML),它在页面上创建了一个按钮,其中nodeValue来自一个名为'的标签。作业ID' (生成的GUID)。
<button id="5f8294ca-fe5a-4da9-847b-da99df999000" onclick="markFinished(this.id)" type="button">Finished</button>
因此将按钮的id返回给函数...
function markFinished(clicked_id)
{
cid = clicked_id;
document.write(cid)
}
XML看起来像这样......
<?xml version="1.0" encoding="utf-8"?>
<CurrentJobs>
<Job>
<JobID>25657287-cc52-415b-8781-be37d5098656</JobID>
<Status>a-current</Status>
</Job>
<Job>
<JobID>5f8294ca-fe5a-4da9-847b-da99df999000</JobID>
<Status>a-current</Status>
</Job>
<Job>
<JobID>05a84779-5801-4645-a7f9-74529ea5298b</JobID>
<Status>a-current</Status>
</Job>
<Job>
<JobID>07df3deb-4935-4504-8822-a73ccea038ae</JobID>
<Status>b-complete</Status>
</Job>
<Job>
<JobID>078c496d-ac60-48e7-b9fe-a0e1f78ff2c5</JobID>
<Status>c-upcoming</Status>
</Job>
<Job>
<JobID>07ec868e-d294-4bb3-807d-00df66f5bab2</JobID>
<Status>c-upcoming</Status>
</Job>
<Job>
<JobID>8bdeee5f-2bf6-4e44-8af8-69f600048dfe</JobID>
<Status>a-current</Status>
</Job>
</CurrentJobs>
我需要一种方法让我的函数将clicked_id与JobID匹配,并用&#39; b-finished&#39;替换nextSibling(Status)。非常感谢任何帮助。
答案 0 :(得分:0)
您可以通过解析XML并遍历节点来实现这一目标。
由于您要查找的值是文本,您必须迭代并检查每个元素,直到找到匹配项,然后获取下一个元素并替换该文本内容
var parser = new DOMParser();
var doc = parser.parseFromString(xml, "application/xml");
function markFinished(clicked_id) {
var cid = clicked_id;
var nodes = doc.getElementsByTagName('JobID'),
match = null;
for (var i=nodes.length; i--;) {
if ( nodes[i].textContent.trim() === clicked_id ) {
match = nodes[i];
break;
}
}
if (match) {
match.nextSibling.textContent = 'b-finished'
}
}