mac*_*mac 2 javascript xml jquery parsing
我有一个 xml 文档(来自提要),我正在从中提取值:
$(feed_data).find("item").each(function() {
if(count < 3) {
//Pull attributes out of the current item. $(this) is the current item.
var title = $(this).find("title").text();
var link = $(this).find("link").text();
var description = $(this).find("description").text();
Run Code Online (Sandbox Code Playgroud)
现在在“描述”中,我需要获取img元素,但这给我带来了一些问题。"description" 是一个常规的字符串元素,似乎我无法调用 ".find()" 方法,那我该怎么办?
我试过打电话.find():
var img = $(this).find("description").find("img");
Run Code Online (Sandbox Code Playgroud)
但这是不行的。img 被包裹在一个跨度中,但我也无法做到这一点。有什么建议?我更愿意避免使用子字符串和正则表达式解决方案,但我不知所措。
我还尝试将“描述”字符串转换为 xml 对象,如下所示:
var parser = new DOMParser();
var desc = parser.parseFromString(test,'text/xml');
$(desc).find("img").each(function() {
alert("something here");
});
Run Code Online (Sandbox Code Playgroud)
但这也行不通。似乎可以,但我收到“文档格式不正确”错误。
尝试将 description 标签的内容包含在一个虚拟 div 中,这对我来说似乎更好,并允许 jQuery.find()按预期工作。
例如
$(feed_data).find("item").each(function() {
if(count < 3) {
//Pull attributes out of the current item. $(this) is the current item.
var title = $(this).find("title").text();
var link = $(this).find("link").text();
var description = '<div>' + $(this).find("description").text() + '</div>';
var image = $(description).find('img');
Run Code Online (Sandbox Code Playgroud)