Javascript jQuery我想向锚标记的链接ID添加一个活动类
我已经写了一个字母导航程序-我有锚链接到h4标签。单击a标记时,我希望具有匹配ID的元素具有一个活动的类。单击另一个锚定标记时,它将删除活动的类并将其指定给另一个图元。以下是我目前掌握的情况:Javascript jQuery我想向锚标记的链接ID添加一个活动类,javascript,jquery,list,Javascript,Jquery,List,我已经写了一个字母导航程序-我有锚链接到h4标签。单击a标记时,我希望具有匹配ID的元素具有一个活动的类。单击另一个锚定标记时,它将删除活动的类并将其指定给另一个图元。以下是我目前掌握的情况: <ul class="no-bullet inline"> <li><a class="scroller" href="#a"><strong>A</strong></a></li>
<ul class="no-bullet inline">
<li><a class="scroller" href="#a"><strong>A</strong></a></li>
<li><a class="scroller" href="#b"><strong>B</strong></a></li>
<li><a class="scroller" href="#c"><strong>C</strong></a></li>
</ul>
<h4 class="alpha-heading" id="a"><strong>A</strong></h4>
<h4 class="alpha-heading" id="b"><strong>B</strong></h4>
<h4 class="alpha-heading" id="c"><strong>C</strong></h4>
$("scroller").on("click", function(){
function matchAlpha(){
var matchID = $(this).attr("href");
find.$(matchID).find.$(alpha-heading).removeClass("active");
find.$(matchID).find.$(this).$(alpha-heading).addClass("active");
}
});
A
B
C
$(“滚动条”)。在(“单击”,函数()上{
函数matchAlpha(){
var matchID=$(this.attr(“href”);
find.$(匹配ID).find.$(alpha标题).removeClass(“活动”);
find.$(matchID).find.$(this.$(alpha heading).addClass(“活动”);
}
});
代码中几乎没有什么东西需要修改
添加到scoller以查找类.alpha-heading
从以前的活动中删除活动。更具体地说,您甚至可以执行.alpha-heading.active
matchID
作为id选择器$(“.scroller”)。在(“单击”,函数()上){
var matchID=$(this.attr(“href”);
//$('.alpha heading').removeClass(“活动”);
$('.alpha heading.active').removeClass(“active”);
$(matchID).addClass(“活动”);
});代码>
.active{
颜色:红色
}
A
B
C
代码中几乎没有什么东西需要修复
将
添加到scoller以查找类
使用.alpha-heading
从以前的活动中删除活动。更具体地说,您甚至可以执行.alpha-heading.active
使用matchID
作为id选择器
$(“.scroller”)。在(“单击”,函数()上){
var matchID=$(this.attr(“href”);
//$('.alpha heading').removeClass(“活动”);
$('.alpha heading.active').removeClass(“active”);
$(matchID).addClass(“活动”);
});代码>
.active{
颜色:红色
}
A
B
C
首先,您需要在滚动条中添加缺少的
,然后使用href
作为选择器为单击的元素添加活动类。以下是代码的更新工作版本:
$(“.scroller”)。在(“单击”,函数()上){
$('h4.alpha heading').removeClass('active');
$($(this.attr('href')).addClass('active');
});代码>
.active{
背景:黄色;
}
A
B
C
首先,您需要在滚动条中添加缺少的
,然后使用href
作为选择器为单击的元素添加活动类。以下是代码的更新工作版本:
$(“.scroller”)。在(“单击”,函数()上){
$('h4.alpha heading').removeClass('active');
$($(this.attr('href')).addClass('active');
});代码>
.active{
背景:黄色;
}
A
B
C
您可以将代码简化为单击处理程序中的一行:
$('.scroller')。单击(函数(){
$($(this.attr('href')).addClass('active').sides().removeClass('active'))
})
.active{
背景:#美国联邦航空局;
}
A
B
C
您可以将代码简化为单击处理程序中的一行:
$('.scroller')。单击(函数(){
$($(this.attr('href')).addClass('active').sides().removeClass('active'))
})
.active{
背景:#美国联邦航空局;
}
A
B
C
匿名事件处理程序函数中有一个命名函数,但从未调用过它。如果该命名函数确实运行了,您将得到几个运行时错误。您可能希望运行一些jQuery教程(可能还有一些基本的JavaScript教程)来获得语法句柄。您的匿名事件处理程序函数中有一个命名函数,但您从未调用过它。如果该命名函数确实运行了,您将得到几个运行时错误。您可能希望运行一些jQuery教程(可能还有一些基本的JavaScript教程)来了解语法。