找到& lt; img>标记在文本字符串内
find <img> tag inside text string
我有一个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();
现在在"描述"中,我需要得到img
元素,但这给我带来了一些问题。"description"是一个常规的字符串元素,似乎我不能调用"。find()"方法,所以我该怎么办?
我试着打电话给.find()
:
var img = $(this).find("description").find("img");
但是不行。img被包装在一个span中,但我也无法做到这一点。有什么建议吗?我宁愿避免子字符串和正则表达式的解决方案,但我在一个损失。
我也试过把"description"字符串变成一个xml对象,像这样:
var parser = new DOMParser();
var desc = parser.parseFromString(test,'text/xml');
$(desc).find("img").each(function() {
alert("something here");
});
但这也不行。看起来是这样的,但是我得到了一个"document not well formed"的错误。
尝试将描述标签的内容封闭在一个虚拟的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');
您好,感谢您的及时回复。我给格雷格打了个勾,因为我确信他的解决方案会起作用,因为原理和我最后得到的是一样的。我的解决方案是这样的:
$(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();
var thumbnail = "";
var temp_container = $("<div></div>");
temp_container.html(description);
thumbnail = temp_container.find("img:first").attr("src");
因此,将字符串包装在div中,然后使用"find()"来获取第一个img元素。我现在有了图像源,可以根据需要使用。
也许你应该尝试将描述文本转换为HTML标签然后尝试通过jquery遍历它
$('<div/>').html($(this).find("description").text()).find('img')
注释: not testing
相关文章:
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- 将html元素插入到文本字符串中,以匹配另一个html字符串
- 转换<a>使用jQuery将文本字符串转换为dom元素
- 数组中的随机文本字符串
- Javascript-使用“”将toDateStringMonth从文本字符串解析为数字字符串;如果“;vs“;开关”;
- javascript高亮文本字符串
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 函数输入上未终止的文本字符串
- 跳过用户输入的文本字符串中的特殊字符,并在 Javascript 中的每个单词后添加连字符
- 如何使用javascript验证文本字符串
- 将部分文本字符串转换为html
- 如何使用javascript在单个正则表达式中递归替换文本字符串中重复的逗号
- 使用PowerShell在JavaScript网页上搜索文本字符串
- 使用jquery感知文本字符串中的时间范围
- 匹配最后一个点之后的文本字符串
- 从段落中删除某些文本字符串
- 如何使用 javascript 将输入到文本框中的变量插入到文本字符串中
- 当页面上存在特定文本字符串时发送 Google Analytics(分析)事件
- JSON 处理 - 向 JSP 发送 JSON 文本字符串,如何在 JSP 中处理
- 如何使用 JavaScript(但不是标签或属性)替换字符串中所有匹配的纯文本字符串