我想使用jQuery并点击()来抓取图像链接的URL,一旦点击它。这个html看起来像这样。使用jquery点击函数抓取图像链接的url
<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>
到目前为止,我使用的JavaScript看起来像这样,但它并没有抓住点击图像链接的网址。该网址需要传递到变量this
所在的位置。
jQuery(function() {
jQuery("#lp-pom-image-167").click(function() {
trackOutboundLink(this, 'AddToCart', 'Bottom CTA');
return false;
});
});
如何将href网址与适当的基础URI相提并论?即。 “http://url.com/page”?
编辑:澄清了我试图拔出。
'$(this).children(“a img”)。attr('src')'应该这样做。 – Supericy 2013-02-19 00:34:52
''希望你不打算复制粘贴这个(任何)JS代码像这样一个200倍的所有'#lp-pom-image-NNN'元素....' ' –
2013-02-19 00:36:55
@Supericy jQuery' .prop'应该用来代替'.attr'。 – Mooseman 2013-02-19 00:40:31