[英]How to grab a link from an anchor tag using jQuery
I'm trying to make a click event where when user clicks on an image, they go to the product detail page. 我正在尝试创建一个单击事件,当用户单击图像时,他们会转到产品详细信息页面。 I have this: 我有这个:
var $randomCartItemName = $($randomCartItem).find(".cart__item__name").html();
which, when console.log, gives me: 这在console.log时给我:
<a href="/us/en_us/shop/a2300?skuId=061006-04&addonSkuId=sku-10-year-standard-warranty"> A2300 </a>
When I click on the div that I want to take me to that page, it takes me to https://www.website.com/us/en_us/<a%20href="/us/en_us/shop/a2500?skuId=061007-04&">A2500</a>
当我单击要带到该页面的div时,它将带我到https://www.website.com/us/en_us/<a%20href="/us/en_us/shop/a2500?skuId=061007-04&">A2500</a>
I read some docs, tried a few different things, and didn't get too far. 我阅读了一些文档,尝试了一些不同的方法,但并没有太过深入。 Any recommendations? 有什么建议吗?
Thanks! 谢谢!
You can simply look for the <a>
element in $randomCartItemName
, retrieve the value of its href
attribute and then redirect the browser to the URL using window.location
: 您可以简单地在$randomCartItemName
查找<a>
元素,检索其href
属性的值,然后使用window.location
将浏览器重定向到URL:
var $randomCartItemName = $($randomCartItem).find(".cart__item__name");
var $link = $randomCartItemname.find('a');
var url = $link.attr('href');
window.location = url;
Of course, you can also chain everything at one go: 当然,您也可以一次性链接所有内容:
var $randomCartItemName = $($randomCartItem).find(".cart__item__name");
window.location = $randomCartItemName.find('a').attr('href');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.