2017-06-23 126 views
-1

这是我的Javascript代码:jQuery的获得HREF链接

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

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

现在什么控制台显示:

a.woocommerce-LoopProduct-link 
accessKey:"" 

而且所有包含在a.wooocommerce-LoopProduct链接

编辑另一件事:你可以在www.cabinepertrattori.com查询网站

+1

你尝试'ATTR( 'href' 属性)'? – prasanth

+1

你可以添加一个你正在使用的标记的例子吗? –

+0

你的HTML代码在哪里? –

回答

2

取而代之的是:

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

+0

我已经尝试过,但它不起作用,它返回与$(this).attr(“src”);' – JustARandomProgrammer

+0

相同的东西检查更新的答案 –

+0

刚刚尝试,工作。 – JustARandomProgrammer

2

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

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

或只是

this.href // (vanilla Javascript, better) 
+0

Upvote为您提供JS解决方案也:P –

+0

我通常preffer香草JS通过jQuery:P – Tomas2D

0

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

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