Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery单击函数获取图像链接的url_Jquery_Url_Click_Href - Fatal编程技术网

使用jquery单击函数获取图像链接的url

使用jquery单击函数获取图像链接的url,jquery,url,click,href,Jquery,Url,Click,Href,我正在尝试使用jquery和click()在单击图像链接后获取其url。html如下所示 <div class="lp-element lp-pom-image" id="lp-pom-image-167" > <a href="http://url.com/page" target=""><img src="/image.jpg" alt=""></a> </div> 如何使用适当的基本URI提取href url?例如:“ 编辑:澄

我正在尝试使用jquery和click()在单击图像链接后获取其url。html如下所示

<div class="lp-element lp-pom-image" id="lp-pom-image-167" >
<a href="http://url.com/page" target=""><img src="/image.jpg" alt=""></a>
</div>
如何使用适当的基本URI提取href url?例如:“

编辑:澄清了我想说的话

jQuery(".lp-pom-image").click(function() {
  console.log($("img", this).attr("src"));
  return false;
}); 
在上查看工作演示

请参阅我建议(尽管未经测试)的

上的工作演示:

我建议(尽管未经测试):


如果您想获得
url,请使用下面的代码而不是
this

$(this).find("a").attr('href')
或者,如果您想要获取
img
src,则使用此代码:

$(this).find("a img").attr('src')

如果您想获得
url,请使用下面的代码而不是
this

$(this).find("a").attr('href')
或者,如果您想要获取
img
src,则使用此代码:

$(this).find("a img").attr('src')

$(this).children(“一个img”).attr(“src”)
应该能做到这一点。
希望你不要打算复制粘贴这样的JS代码200次,粘贴到你所有的
#lp pom image NNN
元素中
@Supericy jQuery
.prop
应该用来代替
.attr
@Mooseman请粘贴一个官方链接,我们可以在这里阅读。@sheikheera特别回答:
$(this)。children(“img”).attr('src')
应该可以做到这一点。
希望你不打算复制粘贴这个(任何)对于所有的
#lp pom图像NNN
元素,像这样的JS代码可以使用200次
@Supericy jQuery
.prop
应该使用,而不是
.attr
@Mooseman请粘贴一个官方链接,我们可以在这里阅读。@Sheikheera特别回答:与我的示例类似,但您使用的是.prop()instad of.attr()。注意:从jQuery1.6开始,.prop()方法提供了一种显式检索属性值的方法,而.attr()则检索属性@马克西姆:是的,我知道。但是谢谢你花时间指出这一点。我假设他想要完整的URL(因此
prop()
),而不是检索属性中可能存在的潜在相对URL(他可以通过
attr()
)获得该URL)。尽管我将find('img')与find('a')切换为find('img'),但这还是有效的。谢谢与我的示例类似,但您使用的是.attr()的.prop()instad。注意:从jQuery1.6开始,.prop()方法提供了一种显式检索属性值的方法,而.attr()则检索属性@马克西姆:是的,我知道。但是谢谢你花时间指出这一点。我假设他想要完整的URL(因此
prop()
),而不是检索属性中可能存在的潜在相对URL(他可以通过
attr()
)获得该URL)。尽管我将find('img')与find('a')切换为find('img'),但这还是有效的。谢谢