JQuery find an img src

2019-03-05 06:58发布

With JQuery I want to be able to get the img src link.

   <tbody>
            <tr>
                <td headers="header_0 header_1">&#160;<img
                    src="https://this-link-here.com" width="26"
                    height="24" alt="" border="0" /></td>

I have tried

   var data = $(data).find("[headers='header_0 header_1']");

but this is not getting the link. Similarly I have tried adding .text(), and .html() but still no luck.

Can anyone help? The HTML cannot be changed.

3条回答
疯言疯语
2楼-- · 2019-03-05 07:38

You can use either the .attr() function:

var data = $(data).find("[headers='header_0 header_1']").find('img').attr("src");

Or the vanilla JS .src function, if there is only one with those headers:

var data = $(data).find("[headers='header_0 header_1']").find('img').get(0).src;

The vanilla JS way is faster, only reason I threw it in.

查看更多
倾城 Initia
3楼-- · 2019-03-05 07:43

Do it like this:

var data = $("img", "td[headers='header_0 header_1']").attr("src");

Demo: http://jsfiddle.net/udeXR/2/

查看更多
该账号已被封号
4楼-- · 2019-03-05 07:49

Use the .attr() function.

var data = $("td[headers='header_0 header_1'] img").attr("src");
  • P.S - I've changed the selector because something doesn't look right in var data = $(data) etc.. You can use a different selector if you wish.

.attr()

.attr( attributeName )

Returns: String

Description: Get the value of an attribute for the first element in the set of matched elements.

查看更多
登录 后发表回答