te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>jquery - Find src attribute value of <img> tag in a Javascript string - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Find src attribute value of <img> tag in a Javascript string - Stack Overflow

programmeradmin2浏览0评论

I have been trying to get the 'src' in img tags inside a javascript variable that contains a block of HTML code dynamically generated/assigned.

Eg:

var post_body = "<div>This is an image <img src='abcd.jpg' /> and this is a paragraph <p>hi there</p> Here we have another image <img src='pqrs.jpg' /></div>"

the content of the 'post' variable is not predefined and it is dynamic and the HTML code in it always changes. To get the src of the image in it, I did the following. But it does not always work for me.

var firstimg = $(post_body).find("img:first").attr("src");

I was trying to get the first image of a blog post from the blog post's content but this does not work for some posts having images. How can this be done using javascript or jQuery without failing?

I have been trying to get the 'src' in img tags inside a javascript variable that contains a block of HTML code dynamically generated/assigned.

Eg:

var post_body = "<div>This is an image <img src='abcd.jpg' /> and this is a paragraph <p>hi there</p> Here we have another image <img src='pqrs.jpg' /></div>"

the content of the 'post' variable is not predefined and it is dynamic and the HTML code in it always changes. To get the src of the image in it, I did the following. But it does not always work for me.

var firstimg = $(post_body).find("img:first").attr("src");

I was trying to get the first image of a blog post from the blog post's content but this does not work for some posts having images. How can this be done using javascript or jQuery without failing?

Share Improve this question edited Oct 6, 2013 at 13:10 logic-unit 4,31312 gold badges49 silver badges74 bronze badges asked Oct 6, 2013 at 10:08 Teshan N.Teshan N. 2,5355 gold badges33 silver badges62 bronze badges 4
  • 2 You are referring to post_body while your variable is called post. – Guillaume Poussel Commented Oct 6, 2013 at 10:10
  • Sorry I have changed the question's text accordingly. 'post' must be changed as 'post_body'. That is why I have used post_body thereafter by a mistake while preparing this question. – Teshan N. Commented Oct 6, 2013 at 12:34
  • 1 It works here: jsfiddle/3SLJm. What is the scenario where it fails? – ced-b Commented Oct 6, 2013 at 13:43
  • Have you found a solution for this? – user706420 Commented Oct 28, 2015 at 9:54
Add a ment  | 

4 Answers 4

Reset to default 8

Using plain JavaScript, dump the HTML into a temporary element and extract it:

var div = document.createElement('div');
div.innerHTML = post_body;
var firstImage = div.getElementsByTagName('img')[0]
var imgSrc = firstImage ? firstImage.src : "";
// or, if you want the unresolved src, as it appears in the original HTML:
var rawImgSrc = firstImage ? firstImage.getAttribute("src") : "";

Change $(post_body) to $(post)

Try

var post = "<div>This is an image <img src='abcd.jpg' /> and this is a paragraph <p>hi there</p> Here we have another image <img src='pqrs.jpg' /></div>";
var firstimg = $(post).find('img:first').attr('src');

Perhaps you can try adding id to img tag and then access it.

var oImg = document.getElementById('myImg');
var attr = oImg.getAttribute('src');

This is a pure js solution.

Late but, if don't want the images to be loaded use the below.

var div = document.createElement('template');
div.innerHTML = post_body;

if you create div element the remote images are loaded the moment you insert innerHTML, template prevents the requests.

发布评论

评论列表(0)

  1. 暂无评论