Javascript jquery获取img src onclick事件
我在引用onclick evet上的图像时遇到问题 我的例子是,“this”引用指向一个元素,该元素有两个图像,我想更改一个特定的图像Javascript jquery获取img src onclick事件,javascript,jquery,html,Javascript,Jquery,Html,我在引用onclick evet上的图像时遇到问题 我的例子是,“this”引用指向一个元素,该元素有两个图像,我想更改一个特定的图像 <li class = "header_det"> <table width="100%" style="font-size:14px;"> <tr> <td width="10%" ali
<li class = "header_det">
<table width="100%" style="font-size:14px;">
<tr>
<td width="10%" align = "right" rowspan = "2">text</td>
<td width="80%"><img width="30" height="30" src="images/map_white_pin.png" ></a></td>
<td width="10%" align = "center" class = "header_pic"><img width="20" height="20" src="images/route_det/down_arrow.png" ></td>
</tr>
</table>
</li>
这段代码不起作用,我必须使用this指针,因为我有另一个var img_type = $(this).attr(".header_pic img").attr("src") ;
应该是:
var img_type = $(this).find(".header_pic img").attr("src") ;
所有jquery都是错误的:
.header\u pic img
不是属性,而是.header\u det\u map
另外,您的HTML有点凌乱:
- 属性名和值之间不应该有空格 HTML
- 有关闭标签
- 您应该关闭
标记”
在您的代码中搜索
header\u det\u map
,但在html中它只是header\u det
。这是正确的还是这不是问题的完整html?还值得注意的是,第一行的第一个.attr
将返回未定义的,因此第二个。attr
将抛出错误非常感谢您的完整答案您的代码是正确的如果是html5,则无需关闭img标记
var img_type = $(this).find(".header_pic img").attr("src") ;
<li class="header_det">
<table width="100%" style="font-size:14px;">
<tr>
<td width="10%" align="right" rowspan="2">text</td>
<td width="80%">
<img width="30" height="30" src="images/map_white_pin.png" />
</td>
<td width="10%" align="center" class="header_pic">
<img width="20" height="20" src="images/route_det/down_arrow.png" />
</td>
</tr>
</table>
</li>
$(".header_det_map").click(function() {
var img = $(this).find(".header_pic img"); // use a variable to store your image element in case you have to change the container class
var img_type = img.attr("src"); // not necessary to store it, could be use as it in the if statement
if(img.attr("src") == "images/route_det/down_arrow.png") {
img.attr("src", "images/route_det/up_arrow.png");
} else {
img.attr("src", "images/route_det/down_arrow.png");
}
});