2017-08-19 59 views
1

我试图让有itemprop="url"所有href值,内部main获取itemprop的href的值与jQuery

HTML:

<div id="main"> 
 
    <div class="list_item odd" itemscope="" itemtype="http://schema.org/Movie"> 
 
     <div class="overview-top"> 
 
      <h4 itemprop="name"><a href="example.com/hello1" itemprop="url"> Hello 1</a></h4> 
 
     </div> 
 
    </div> 
 
    <div class="list_item even" itemscope="" itemtype="http://schema.org/Movie"> 
 
     <div class="overview-top"> 
 
      <h4 itemprop="name"><a href="example.com/hello2" itemprop="url"> Hello 2</a></h4> 
 
     </div> 
 
    </div> 
 
</div>

JQuery的:

$(('meta[itemprop="url"]'), '#main').each(function(){ 
 
    var url = $(this).attr('href'); 
 
    console.log (url); 
 
});

回答

0

你不需要选择器中的'meta`部分。

$(('[itemprop="url"]'), '#main').each(function(){ 
 
      var url = $(this).attr('href'); 
 
      console.log (url); 
 
     }); 
 
    
<div id="main"> 
 
    <div class="list_item odd" itemscope="" itemtype="http://schema.org/Movie"> 
 
     <div class="overview-top"> 
 
      <h4 itemprop="name"><a href="example.com/hello1" itemprop="url"> Hello 1</a></h4> 
 
     </div> 
 
    </div> 
 
    <div class="list_item even" itemscope="" itemtype="http://schema.org/Movie"> 
 
     <div class="overview-top"> 
 
      <h4 itemprop="name"><a href="example.com/hello2" itemprop="url"> Hello 2</a></h4> 
 
     </div> 
 
    </div> 
 
</div>

0

像这样改变选择器$('#main').find('a[itemprop="url"]')

  1. find()目标主。适用的ATTR选择与itemprop="url"
  2. 没有必要的innerElement与在选择添加meta

$('#main').find('a[itemprop="url"]').each(function() { 
 
    var url = $(this).attr('href'); 
 
    console.log(url); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="main"> 
 
    <div class="list_item odd" itemscope="" itemtype="http://schema.org/Movie"> 
 
    <div class="overview-top"> 
 
     <h4 itemprop="name"><a href="example.com/hello1" itemprop="url"> Hello 1</a></h4> 
 
    </div> 
 
    </div> 
 
    <div class="list_item even" itemscope="" itemtype="http://schema.org/Movie"> 
 
    <div class="overview-top"> 
 
     <h4 itemprop="name"><a href="example.com/hello2" itemprop="url"> Hello 2</a></h4> 
 
    </div> 
 
    </div> 
 
</div>

0

您可以使用overview-top类作为一个jQuery选择,然后属性选择为[itemprop="url"]find

$('.overview-top').find('[itemprop="url"]').each(function(){ 
 
    var url = $(this).attr('href'); 
 
    console.log (url); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="main"> 
 
    <div class="list_item odd" itemscope="" itemtype="http://schema.org/Movie"> 
 
     <div class="overview-top"> 
 
      <h4 itemprop="name"><a href="example.com/hello1" itemprop="url"> Hello 1</a></h4> 
 
     </div> 
 
    </div> 
 
    <div class="list_item even" itemscope="" itemtype="http://schema.org/Movie"> 
 
     <div class="overview-top"> 
 
      <h4 itemprop="name"><a href="example.com/hello2" itemprop="url"> Hello 2</a></h4> 
 
     </div> 
 
    </div> 
 
</div>

0

HTML

<a href="http://example.com/1" class="link">Example</a> 
<a href="http://example.com/2" class="link">Example</a> 

jQuery的

$(document).ready(function() { 
    const array = []; 
    $('.link').each(function() { 
    array.push($(this).attr('href'); 
    }); 
}); 

小提琴: https://jsfiddle.net/9fg6bb85/1/

0

jQuery的不能与属性itemprop所以你的例子不工作找到元素meta。您可以搜索具有属性itemprop在我的例子中的任何元素...或代替meta设置a元件,它会发现只有链接元素

$(function(){ 
 
    $('[itemprop="url"]', '#main').each(function(){ 
 
    console.log($(this).attr('href')); 
 
    }) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="main"> 
 
    <div class="list_item odd" itemscope="" itemtype="http://schema.org/Movie"> 
 
     <div class="overview-top"> 
 
      <h4 itemprop="name"><a href="example.com/hello1" itemprop="url"> Hello 1</a></h4> 
 
     </div> 
 
    </div> 
 
    <div class="list_item even" itemscope="" itemtype="http://schema.org/Movie"> 
 
     <div class="overview-top"> 
 
      <h4 itemprop="name"><a href="example.com/hello2" itemprop="url"> Hello 2</a></h4> 
 
     </div> 
 
    </div> 
 
</div>