gpt4 book ai didi

javascript - 使用 JQuery 获取 itemprop 的 href 值

转载 作者:太空宇宙 更新时间:2023-11-04 15:26:53 26 4
gpt4 key购买 nike

我正在尝试获取 main div 内具有 itemprop="url" 的所有 href

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

最佳答案

您的选择器中不需要“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>

关于javascript - 使用 JQuery 获取 itemprop 的 href 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45770929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com