Javascript 如何将jQuery代码重新写入ES6?
其思想是获取所有链接和响应ID,隐藏非“活动”的内容。单击时,锚定链接应打开相应的内容 加价 上面的脚本代码可以工作,挑战是将其重新写入ES6(我在下面已经完成了,但没有按预期工作)。是一个JSFIDLE代码,用于说明我所面临的问题Javascript 如何将jQuery代码重新写入ES6?,javascript,jquery,ecmascript-6,Javascript,Jquery,Ecmascript 6,其思想是获取所有链接和响应ID,隐藏非“活动”的内容。单击时,锚定链接应打开相应的内容 加价 上面的脚本代码可以工作,挑战是将其重新写入ES6(我在下面已经完成了,但没有按预期工作)。是一个JSFIDLE代码,用于说明我所面临的问题 $("ul.navTabs > li").each((index, element) => { // keep track of tabs and associated content const anchorTag = $(element).f
$("ul.navTabs > li").each((index, element) => {
// keep track of tabs and associated content
const anchorTag = $(element).find("a");
let active = anchorTag;
let content = anchorTag;
const links = anchorTag;
// if identifyUrlHash matches one of the links, use that as active tab
// if no match found, use first link as initial active tab
const identifyUrlHash = window.location.hash;
active = $(links.filter(`[href="${identifyUrlHash}"]`)[0] || links[0]);
active.addClass("active");
content = $(active[0].hash);
// hide the remaining content
links.not(active).each(element, (subindex, subelement) => {
$(subelement.hash).hide();
});
$(element).on("click", "a", (event) => {
event.preventDefault();
// old tab inactive
active.removeClass("active");
content.hide();
// update variables with new link and content
active = $(element);
content = $(element.hash);
// make tab active
element.addClass("active");
content.show();
});
});
- 在链接和窗格上使用class
李>.active
- 不要为li元素设置样式。在A元素中使用
active
- 使用JS的方法(或在jQuery中)
const$tabs=$(“.navbar a”);
const$panes=$(“.navcontent.content”);
常量手柄=(ev)=>{
const hash=ev?ev.currentTarget.hash:window.location.hash;
如果(!hash)return;//没有哈希,请使用内联的“active”类
$tabs.each((i,el)=>$(el.toggleClass(“active”,el.hash==hash));
$panes.each((i,el)=>$(el.toggleClass(“active”,el.id===hash.replace(“#“,”));
}
$tabs.on(“点击”,扶手);
扶手板();//在初始化时,页面加载、读取哈希(如果有)并使其处于活动状态
.navbar导航ul{
保证金:0;
填充:0;
显示器:flex;
flex-flow:行nowrap;
列表样式类型:无;
}
/*PS不要过分夸张LI元素对待他们就像对待TD元素一样*/
.导航栏导航a{
文字装饰:无;
显示:块;
填充物:5px10px;
}
.navbar nav a.active{/*样式的内部元素*/
底部边框:2件纯黑;
}
/*窗格样式*/
.navcontent.content{
显示:无;
}
.navcontent.content.active{
显示:块;
}
-
-
-
表1
11111洛雷姆·伊普苏姆·多洛尔·希特,一位杰出的献身者,在劳动和生活的临时契约中。但是,在最低限度上,我们需要一个实验室来进行日常工作。酒后驾车
这是一种不受欢迎的行为,它是一种不受欢迎的行为。除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡
表2
22222 Lorem ipsum door sit amet,一位杰出的献身者,他是一位临时劳工和职业经理人。但是,在最低限度上,我们需要一个实验室来进行日常工作。二人世界
这是一种令人厌恶的行为,因为它是一种不平等的行为。除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡。
表3
33333 Lorem ipsum door sit amet,供职于社会精英,在劳动和生活中暂时占有一席之地。但是,在最低限度上,我们需要一个实验室来进行日常工作。二人世界
这是一种令人厌恶的行为,因为它是一种不平等的行为。除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡。
问题是什么?您发布的代码中有什么不起作用?这与ES6有什么特别的关系?您在ES5中有工作代码吗?@Bergi请查看JSFIDLE代码以了解我的问题。没有,请描述问题。什么不按预期工作?您所说的“在ES6中”具体是什么意思?你只想使用箭头函数?你想完成什么?定义“不起作用”。你为什么需要JQ?@njuguunaureIti这是一个剩菜。Removed为什么要将代码从jQuery重写为DOM方法?这不是OP所要求的。@Bergi hum-ham给人的印象是,他完全摆脱了jQuery,只专注于一个特定的问题。无论如何。。。
$('ul.navTabs').each(function() {
var $active, $content, $links = $(this).find('a');
$active = $($links.filter('[href="' + location.hash + '"]')[0] || $links[0]);
$active.addClass('active');
$content = $($active[0].hash);
$links.not($active).each(function() {
$(this.hash).hide();
});
$(this).on('click', 'a', function(e) {
e.preventDefault();
$active.removeClass('active');
$content.hide();
$active = $(this);
$content = $(this.hash);
$active.addClass('active');
$content.show();
});
});
$("ul.navTabs > li").each((index, element) => {
// keep track of tabs and associated content
const anchorTag = $(element).find("a");
let active = anchorTag;
let content = anchorTag;
const links = anchorTag;
// if identifyUrlHash matches one of the links, use that as active tab
// if no match found, use first link as initial active tab
const identifyUrlHash = window.location.hash;
active = $(links.filter(`[href="${identifyUrlHash}"]`)[0] || links[0]);
active.addClass("active");
content = $(active[0].hash);
// hide the remaining content
links.not(active).each(element, (subindex, subelement) => {
$(subelement.hash).hide();
});
$(element).on("click", "a", (event) => {
event.preventDefault();
// old tab inactive
active.removeClass("active");
content.hide();
// update variables with new link and content
active = $(element);
content = $(element.hash);
// make tab active
element.addClass("active");
content.show();
});
});