使用JQuery获取itemprop的href值

时间:2017-08-19 11:23:12

标签: javascript jquery html

我想在href div

内获取所有itemprop="url"值为main的{​​{1}}

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);
});

5 个答案:

答案 0 :(得分:0)

更改选择器,如$('#main').find('a[itemprop="url"]')

  1. find ()定位主要innerElement。使用itemprop="url"
  2. 应用attr选择器
  3. 无需在选择器中添加meta
  4. &#13;
    &#13;
    $('#main').find('a[itemprop="url"]').each(function() {
      var url = $(this).attr('href');
      console.log(url);
    });
    &#13;
    <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>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

您不需要选择器中的“meta”部分。

&#13;
&#13;
$(('[itemprop="url"]'), '#main').each(function(){
            var url = $(this).attr('href');
            console.log (url);
        });
        
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以将overview-top类用作JQuery选择器,然后将[itemprop="url"]

中的属性选择器用作find

&#13;
&#13;
$('.overview-top').find('[itemprop="url"]').each(function(){
    var url = $(this).attr('href');
    console.log (url);
});
&#13;
<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>
&#13;
&#13;
&#13;

答案 3 :(得分: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/

答案 4 :(得分:0)

Jquery无法找到属性为meta的元素itemprop,因此您的示例无效。您可以在我的示例中搜索具有属性itemprop的任何元素...或者代替meta设置a元素,而该元素只会找到链接元素

&#13;
&#13;
$(function(){
  $('[itemprop="url"]', '#main').each(function(){
    console.log($(this).attr('href'));
  })
})
&#13;
<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>
&#13;
&#13;
&#13;

相关问题