简体   繁体   English

如何将此代码从 Jquery 转换为 Vanilla Javascript?

[英]How to translate this code from Jquery to Vanilla Javascript?

I need to translate this code from Jquery to Vanilla JS.我需要将此代码从 Jquery 转换为 Vanilla JS。 I provide you all code but only is an important JS file.我为您提供了所有代码,但只是一个重要的 JS 文件。 HTML and scss files not important, maybe only for catching class and nodes. HTML 和 scss 文件并不重要,也许仅用于捕获类和节点。 I will provide and codepen code: https://codepen.io/lakialex/pen/eqvEBR .我将提供和 codepen 代码: https ://codepen.io/lakialex/pen/eqvEBR。 For all question i am here对于所有问题,我都在这里

HTML: 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: 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');
      }
    });
});

This should do the trick这应该可以解决问题

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

However, as other have stated in comments, you should probably learn to search for this on your own.但是,正如其他人在评论中所说的那样,您可能应该学会自己搜索。 You can find more informations on the methods I used to do this here :您可以在此处找到有关我用来执行此操作的方法的更多信息:

querySelectorAll 查询选择器全部

parentNode 父节点

addEventListener 添加事件监听器

previousElementSibling 以前的元素兄弟姐妹

nextElementSibling 下一个元素兄弟姐妹

classList 类列表

You can also find all of jQuery source code on GitHub, which might help you understand how they do things comprehensively !您还可以在 GitHub 上找到所有jQuery 源代码,这可能有助于您全面了解它们是如何做事的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM