gpt4 book ai didi

javascript - 如何访问 的 href 属性以根据要求比较 url?

转载 作者:行者123 更新时间:2023-12-02 19:33:38 27 4
gpt4 key购买 nike

<style>
.aaaa{
background:red;
}
</style>

<div class="subnav_catg">
<article id="anything" class="catg_list"><a class="catg_photo" href="http://www.xyz.com/1"> <img src="http://www.xyz.com/11.jpg"> </a> <hgroup>
<h1 class="catg_title">
Make it</h1>
</hgroup> </article>
<article id="anything" class="catg_list"><a class="catg_photo" href="http://www.xyz.com/2"> <img src="http://www.xyz.com/65.jpg"> </a> <hgroup>
<h1 class="catg_title">
Chocolate</h1>
</hgroup> </article>
<article id="anything" class="catg_list"><a class="catg_photo" href="http://www.xyz.com/3"> <img src="http://www.xyz.com/79.jpg"> </a> <hgroup>
<h1 class="catg_title">
Divine</h1>
</hgroup> </article>
</div>

<script>
var url_string = document.URL;
var mainarticle = document.querySelectorAll('.subnav_catg');
for (var i = 0; i <= mainarticle.length; i++) {
var current_article = mainarticle[i];
if (url_string == current_article.firstChild.attributes[href]) {
var theParagraph = getElementsByClassName('catg_list');
theParagraph.setAttribute('id', 'aaaa');
}
}
</script>

在上面的代码中,我将当前网址与每篇文章中的“a”标签进行比较(如果匹配),然后将 id 从“anything”替换为“aaaa”但我没有得到。那么出了什么问题?

最佳答案

current_article.firstChild.attributes[href] 肯定应该是 current_article.firstChild.attributes['href']current_article.firstChild.attributes.href

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