繁体   English   中英

jquery获取href链接

[英]Jquery get href link

这是我的 Javascript 代码:

$(".woocommerce-LoopProduct-link").each(function(){
        var str=$(this).attr("src");    
        console.log(str);
    });

例如,我想看到一个包含“ http://mywebsitelinked.com ”的字符串。

现在控制台显示的是:

a.woocommerce-LoopProduct-link
accessKey:""

以及 a.wooocommerce-LoopProduct-link 中包含的所有其他内容

取而代之的是:

$(this).attr("src");

试试

$(this).attr("href");

当您尝试获取URLURL位于锚点的href属性中。

例如:

 $(document).ready(function(){ alert($('a').attr('href')); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="http://google.com/">Google></a>

如果您使用的是链接元素,则必须使用

$(this).prop("href")` or `$(this).attr("href")

或者只是

this.href // (vanilla Javascript, better)

尝试:这将匹配所有与 mywebsitelinked.com 的 hrefs

$(".woocommerce-LoopProduct-link").each(function(){
  var str=$(this).attr("href");
  if(str.match(/mywebsitelinked.com/gi)!=""){
    console.log(str);
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM