![](/img/trans.png)
[英]Get only the first level children of type in javascript or jquery
[英]jQuery : find in first level children only (XML)
我们如何限制查找仅在XML文档中的一级子级中进行搜索。
例如:
考虑一下这个XML,它是Onet网络服务( https://services.onetcenter.org/developer/ )的响应。
<?xml version="1.0" encoding="UTF-8"?>
<report code="27-2011.00">
<career>
<code>13-2011.01</code>
<title>Accountants</title>
<tags bright_outlook="true" green="false" apprenticeship="false"/>
<also_called>
<title>Accountant</title>
<title>Accounting Manager</title>
<title>Certified Public Accountant (CPA)</title>
<title>Staff Accountant</title>
</also_called>
<what_they_do>Analyze financial information and prepare financial reports to determine or maintain record of assets, liabilities, profit and loss, tax liability, or other financial activities within an organization.</what_they_do>
<on_the_job>
<task>Prepare, examine, or analyze accounting records, financial statements, or other financial reports to assess accuracy, completeness, and conformance to reporting and procedural standards.</task>
<task>Report to management regarding the finances of establishment.</task>
<task>Establish tables of accounts and assign entries to proper accounts.</task>
</on_the_job>
<resources>
<resource href="https://services.onetcenter.org/ws/mnm/careers/13-2011.01/knowledge">
<title>Knowledge</title>
</resource>
..........................
..........................
.............................
</explore_more>
<where_do_they_work>
<industry href="https://services.onetcenter.org/ws/mnm/browse/54" percent_employed="33">
<code>54</code>
<title>Professional, Science, & Technical</title>
</industry>
</where_do_they_work>
</report>
在这里,我试图将这些数据布置在模式对话框上。 因此,出于这个目的,我需要处理每个节点。
所以首先我需要找到标题。 所以我有尝试的这段代码。
var career = $(xml).find('career');
然后我将标题标签文本内容分配给标题字段
因此,我使用
$('#myh4').html($(career).find('title').text());
但显然,它会将所有标题标签文本分配给标题。
因此,简单的问题是如何将find函数限制为仅第一级子级。 是指如何防止它咬入下一级别孩子喜欢also_called
使用.children('title').text()
代替 。
.children()方法与.find()的不同之处在于,.children()仅沿DOM树向下移动一个级别,而.find()可以向下遍历多个级别以选择后代元素(孙子等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.