我想在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);
});
答案 0 :(得分:0)
更改选择器,如$('#main').find('a[itemprop="url"]')
。
find ()
定位主要innerElement
。使用itemprop="url"
meta
$('#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;
答案 1 :(得分:0)
您不需要选择器中的“meta”部分。
$(('[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;
答案 2 :(得分:0)
您可以将overview-top
类用作JQuery选择器,然后将[itemprop="url"]
find
$('.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;
答案 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');
});
});
答案 4 :(得分:0)
Jquery无法找到属性为meta
的元素itemprop
,因此您的示例无效。您可以在我的示例中搜索具有属性itemprop
的任何元素...或者代替meta
设置a
元素,而该元素只会找到链接元素
$(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;