我有以下html字符串(var jsonHtmlString)作为jQuery返回的jquery get请求,并且需要解析html以返回使用以下信息。有可能是一个简单的方法来做到这一点,但我一直无法找到它。我希望有人能够推荐一个“轻松解析”解决方案(并且如果您使用在线工具(例如plnkr.co)在测试您的js代码时轻松查看解析)?jQuery - 为html字符串“轻松解析”的好解决方案
我已经包括jQuery选择器值应该在映射对象的每一行上返回什么。谢谢!
var jsonHtmlString = '<div class="search-result-srch box"> <span class="number">1.</span>
<a href="/Practice/Detail.aspx?docid=18600&mode=search&[email protected]">The Continuous Learning Technology Stack: Thinking outside the LMS</a>
<br />
<div title="Research Report" class="description rr">This report introduces the Continuous Learning Technology Stack, or the sum of technologies that can be leveraged for enabling continuous learning within organizations. </div>
<div class="pracsub"> <span class="col1-a">Practice:</span> <span class="col1-b"><strong>Tools & Technology</strong></span> <span class="col2-a">Subject:</span> <span class="col2-b"><strong>Learning Tech</strong></span> </div>
<div class="pracsub top-blue-brdr"> <span class="col1-a">Access:</span> <span class="col1-b">Member</span> <span class="col2-a">Published:</span> <span class="col2-b">09/02/2015</span>
<div class="clearfix"></div> <span class="col1-a">Type:</span> <span class="col1-b">Research Report</span> </div>
<div class="clearfix"></div>
</div>
<div class="search-result-srch box"> <span class="number">2.</span>
<a href="/Practice/Detail.aspx?docid=18817&mode=search&[email protected]">Business Case for Investing Beyond the LMS (editable PPT)</a>
<br />
<div title="Performance Support" class="description ps">This editable PowerPoint template guides you through the process of building and presenting a business case for investing in continuous learning—beyond the LMS.</div>
<div class="pracsub"> <span class="col1-a">Practice:</span> <span class="col1-b"><strong>Learning & Development</strong></span> <span class="col2-a">Subject:</span> <span class="col2-b"><strong>Informal Learning</strong></span> </div>
<div class="pracsub top-blue-brdr"> <span class="col1-a">Access:</span> <span class="col1-b">Complimentary</span> <span class="col2-a">Published:</span> <span class="col2-b">08/26/2015</span>
<div class="clearfix"></div> <span class="col1-a">Type:</span> <span class="col1-b">Performance Support</span> </div>
<div class="clearfix"></div>
</div>';
var items = parseHtml(jsonHtmlString);
function parseHtml(html) {
var items = $(div.search-result-srch); // array of object for each div class="search-result-srchbox"
items.map((item) => {
return {
title: $(item)('a'), // The Continuous Learning..., Business Case for...
description: $(item)('.description'), // This report introduces..., This editable PowerPoint...
practice: $(item)('.col1-b').innerText, // Tools & Technology, Learning & Development
subject: $(item)('.col2-b').innerText, // Learning Tech, Informal Learning
access: $(item)('.pracsub .col2-b').innerText, // Member, Complimentary
publishDate: $(item)('.pracsub .col2-b').innerText, // 09/02/2015, 08/26/2015
type: $(item)('.pracsub .col1-b:last-of-type').innerText, // Research Report, Performance Support
}
});
}
当你说“解析”时,你是否试图做这样的事情? https://plnkr.co/edit/dRugzmUOXiBGiiDpwoNP?p=preview – aquinas
这正是我的意思!谢谢。我错过了.find参考。一个问题,但。在你选择了items数组的地方,var items = element.find('div.search-result-srch')不是已经在jquery中“包装”的项目(因为在初始选择器中使用了$对象,让元素= $(html))。那么为什么在迭代地图循环中的每个项目时需要引用$(item)? (而不是只使用item.find())?另外,你是否想把你的答案链接到答案中,这样我就可以将它标记出来? –