我需要将此代码从Jquery转换为Vanilla JS。我提供了所有代码,但只有一个重要的JS文件。 HTML和scss文件并不重要,可能仅用于捕获类和节点。我将提供和Codepen代码: https://codepen.io/lakialex/pen/eqvEBR。对于所有问题,我都在这里
HTML:
<div class="dropdown">
<button class="dropbtn dropbtn-three">
DropDown Parent
<i class="fa fa-angle-down"></i>
</button>
<div class="dropdown-content">
<ul>
<li class="item-has-children">
<a href="#0" class="padd-left">List 1 <span class="rightt"> </span></a>
<ul class="sub-menu">
<li><a href="#0">List 2 sub menu <span class="rightt"> </span></a>
<ul class="sub-menu">
<li><a href="Dropdown3/Wahlergebnisse-der-Landtagswahl-2017/Zielsetzungen/entwicklung-zu-einem-souveränen-staat.html">List 3 sub menu </a></li>
<div class="hr2"></div>
<li><a href="Dropdown3/Wahlergebnisse-der-Landtagswahl-2017/Zielsetzungen/verpflichtung-zu-internationaler-solidarität.html">List 3 sub menu </a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
SCSS:
$facebookBlue: #153161;
$strongRed: #9a0000;
$strongYellow: #000;
$white: #ffffff;
* {
box-sizing: border-box;
}
body {
display: flex;
padding-top: 40px;
}
.dropdown {
position: relative;
display: inline-block;
margin: 0 auto;
.dropbtn {
background-color: $facebookBlue;
color: $white;
font-size: 17px;
font-weight: 600;
border: none;
cursor: pointer;
height: 55px;
background: #153161;
border-bottom-left-radius: 7px;
border-bottom-right-radius: 7px;
padding: 12px 50px;
position: relative;
width: 360px;
text-align: left;
i {
margin-left: 30px;
color: #8391ab;
// opacity: 0.2;
position: absolute;
top: 50%;
right: 25px;
transform: translateY(-50%);
}
.arrow {
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid #8191aa;
margin: 100%;
padding-top: 4px;
z-index: 999;
}
}
.dropbtn-two {
background: $strongRed;
}
.dropbtn-three {
background: $strongYellow;
}
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
width: 330px;
z-index: 999;
a {
color: black;
padding: 12px 25px;
text-decoration: none;
display: flex;
justify-content: flex-start;
width: 100%;
&:hover {
background-color: #F8F8F8
}
.rightt {
display: inline-block;
// width: 9px;
// height: 9px;
cursor: pointer;
// padding-left: 180px;
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
z-index: 999;
&:after {
content: '';
display: inline-block;
width: 9px;
height: 9px;
border-top: 0.2em solid #ababab;
border-right: 0.2em solid #ababab;
-moz-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
}
&.selected {
padding-left: 70px;
.rightt {
left: 40px;
top: 25%;
right: auto;
transform: translateY(0);
transform: rotate(180deg);
}
}
}
.left {
display: inline-block;
cursor: pointer;
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
z-index: 999;
&:after {
content: '';
display: inline-block;
width: 29px;
height: 29px;
border-top: 0.2em solid #ababab;
border-right: 0.2em solid #ababab;
-moz-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
}
.item-has-children {
a {
position: relative;
}
}
}
.dropdown:hover .dropdown-content {
display: block;
background: white;
opacity: 0.95;
width: 100%;
}
.hr2 {
height: 1px;
background: #ccc;
border-bottom: 1px solid #fff;
border-top: 1px solid #ccc;
}
.sub-menu {
display: none;
}
.sub-menu.selected {
display: flex;
flex-direction: column;
transition: transform 0.6s;
}
ul {
list-style: none;
padding: 0 0px;
width: 100%;
height: 100%;
}
JS:
const links = $('.item-has-children a');
links.each(function() {
$(this).on('click', function() {
if ($(this).hasClass('selected')) {
$(this).parent().parent().prev().show();
$(this).next('ul').hide();
$(this).removeClass('selected');
} else {
$(this).parent().parent().prev().hide();
$(this).next('ul').show();
$(this).addClass('selected');
}
});
});
答案 0 :(得分:0)
这应该可以解决问题
document.querySelectorAll(".item-has-children a").forEach(element => {
element.addEventListener('click', () => {
if (element.classList.contains('selected')) {
const parentsibling = element.parentElement.parentElement.previousElementSibling;
if (parentsibling) parentsibling.style.display = 'block';
let nextSibling = element;
while (nextSibling !== null && nextSibling.nodeName !== 'UL') {
nextSibling = nextSibling.nextElementSibling;
}
if (nextSibling) nextSibling.style.display = 'none';
element.classList.remove('selected');
} else {
const parentsibling = element.parentElement.parentElement.previousElementSibling;
if (parentsibling) parentsibling.style.display = 'none';
let nextSibling = element;
while (nextSibling !== null && nextSibling.nodeName !== 'UL') {
nextSibling = nextSibling.nextElementSibling;
}
if(nextSibling) nextSibling.style.display = 'block';
element.classList.add('selected')
}
})
})
但是,正如其他人在评论中指出的那样,您可能应该学会自己搜索。您可以在此处找到有关我用来执行此操作的方法的更多信息:
您还可以在GitHub上找到所有jQuery source code,这可能有助于您全面了解它们的工作方式!
答案 1 :(得分:0)
您可以尝试使用它
const linksContainer = document.querySelectorAll(".item-has-children");
linksContainer.addEventListener('click', onClick);
function onClick(event) {
const element = event.target;
if (element.classList.contains('selected')) {
const parentsibling = element.parentElement.parentElement.previousElementSibling;
if (parentsibling) parentsibling.style.display = 'block';
let nextSibling = element;
while (nextSibling !== null && nextSibling.nodeName !== 'UL') {
nextSibling = nextSibling.nextElementSibling;
}
if (nextSibling) nextSibling.style.display = 'none';
element.classList.remove('selected');
} else {
const parentsibling = element.parentElement.parentElement.previousElementSibling;
if (parentsibling) parentsibling.style.display = 'none';
let nextSibling = element;
while (nextSibling !== null && nextSibling.nodeName !== 'UL') {
nextSibling = nextSibling.nextElementSibling;
}
if(nextSibling) nextSibling.style.display = 'block';
element.classList.add('selected')
}
}