2017-08-19 58 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>