简体   繁体   English

jquery获取href链接

[英]Jquery get href link

This is my Javascript code:这是我的 Javascript 代码:

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

What I want to see is a string that contain " http://mywebsitelinked.com " for example.例如,我想看到一个包含“ http://mywebsitelinked.com ”的字符串。

Now what the console display is:现在控制台显示的是:

a.woocommerce-LoopProduct-link
accessKey:""

And all the other thing contained in a.wooocommerce-LoopProduct-link以及 a.wooocommerce-LoopProduct-link 中包含的所有其他内容

Instead of this:取而代之的是:

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

try试试

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

as you are trying to get the URL and URL is in the href attribute of the anchor.当您尝试获取URLURL位于锚点的href属性中。

Ex:例如:

 $(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>

If you are using link element you have to use如果您使用的是链接元素,则必须使用

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

or just或者只是

this.href // (vanilla Javascript, better)

try: this will match all the hrefs with mywebsitelinked.com尝试:这将匹配所有与 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