使用jQuery获取图像src
Get image src using jQuery
我试图获得一个html图像的src
属性。似乎我可以在DOM中找到img
元素,但src
属性未定义,有什么想法吗?
<table id="comboTable" style="width: 100%; height: 100%; margin-bottom: 10px;" data-bind="click: $root.selectCombo">
<tr>
<td>
<img class="selector" src="~/Images/roundOffButton.png" style="position: absolute;margin-left: -57px; margin-top: -35px;" />
</td>
<td style="height: 35px;">
<table style="width: 100%; background-color: #0097D8;padding:5px;">
<tr style="padding:5px;">
<td style="height: 36px; padding-left: 14px; font-weight: bold;color: black; margin: 1px; width: 70%; text-align:left;">
<span data-bind="text: Description" style="padding-left: 5px;" />
</td>
<td style="width: 30%;padding:8px; text-align: center;color: white; background-color:black;">
<span data-bind="text: formatCurrency(Price)" style="font-size: 14px; white-space: nowrap;
font-weight: bold;" />
</td>
</tr>
</table>
</td>
</tr>
</table>
Javascript: var comboSpan = $("span:contains('" + description + "')");
var img = $(comboSpan).closest(".selector");
alert('Image object - ' + img);
var src = $(img).attr('src');
alert(src); //src is undefined
工作演示
-
使用有效的
img
选择器,img
和span
有不同的父级,所以在这种情况下你不能直接使用closest()
-
另外
comboSpan,img
已经是一个jQuery对象。你不需要像$(comboSpan)
-
使用
.closest('#comboTable')
作为你的层次结构中有一个表<table> <tr> <td> <img> //You need this. </td> <td> <table> <tr> <td> <span> //You have this. </td> </tr> </table> </td> </tr> </table>
最终代码:
var comboSpan = $("span:contains('" + description + "')");
var img = comboSpan.closest('#comboTable').find('.selector'); //Change this line
alert('Image object - ' + img);
var src = img.attr('src');
alert(src); //src is undefined
你需要找到最近的tr
和.selector
img:
var comboSpan = $("span:contains('" + description + "')");
var img = $(comboSpan).closest("tr").find(".selector");// find tr and then img
alert('Image object - ' + img);
var src = $(img).attr('src');
alert(src); //src is undefined
您首先尝试找到tr
,然后它的子img
(即.selector
)
var img = $(comboSpan).closest('tr').find('.selector');
试试这个:
var comboSpan = $("span:contains('" + description + "')");
var img = $(comboSpan).closest('tr').find('.selector');
alert('Image object - ' + img);
var src = $(img).attr('src');
alert(src);
使用
var img = comboSpan.closest('tr').find('.selector');
// no need to wrap comboSpan with jquery as comboSpan is already a jquery object
看看这个。
$('.img1 img').attr('src');
只需如下所示:
var src = $('.selector').attr('src');
alert(src);
我的答案是爱
$('.img1 img').attr('src');
相关文章:
- Javascript将图像src更改为淡入淡出
- 图像可以从源<img src=""/>.TEXT可以在没有javascript的情况下从外部
- 在字符串中查找所有图像 src URL 并存储在数组中
- 使用blob作为src图像
- 如何使用javascript和xpath获取图像src
- 如何通过函数调用设置图像的src
- 如何将JSON结果设置为'src'共'img'以在ASP.NET MVC4中显示图像
- 使用带有变量的javascript更改图像src
- 用jquery抓取图像SRC-attr
- 无法通过JQuery更改图像的src
- 如果我使用 src 而不是 ng-src,角度如何仍然渲染图像
- js:如何隐藏/加密图像的src
- MVC5更改jquery脚本中的图像src
- 为什么innerHTML不更改图像的src
- 获取子图像的src
- 隐藏或删除图像 src=未定义
- phantomjs使用src图像在canvas.toDataURL上抛出DOM异常18
- 如果ng-src图像属性抛出错误,是否有办法抑制控制台错误?
- 更改SRC +图像标题
- 如何用新图像替换onerror SRC图像