【发布时间】:2017-02-20 13:25:34
【问题描述】:
我可以使用以下代码搜索 XML 提要
$($xml).find('item').each(function(i, e) { ... }
对于此循环中的每个项目,我可以使用以下几行访问我的 content:encoded 元素并查看结果...
$mcontent = $(this).find('encoded').text();
console.log('source: ' + $mcontent.match(/<img[^>]*>/g));
...其中包含多个用逗号分隔的图像...
<p><img src="...">, <img src="...">, <img src="..."></p>
如何仅获取第一个图像元素,然后将其存储到 $mImage 变量中?
谢谢!
更新:
当我尝试执行以下操作时...
$mcontent = $(this).find('encoded').text();
$mcontent = $mcontent.match(/<img[^>]*>/g).split(',')[0];
console.log('source: ' + $mcontent);
浏览器告诉我...
Uncaught TypeError: $mcontent.match(...).split is not a function
..我不知道为什么...
【问题讨论】:
-
"your-image,string, here".split(',')[0] 会给你第一张图片作为字符串。
-
我上面还有一个问题,在尝试实现这一点时......我也忘记了我原来的问题中的一个元素......
-
感谢 axel 关于拆分的提醒
标签: jquery xml-parsing