将jQuery脚本转换为纯JavaScript



html:

<button id='etiquette-xs-button' type="button" class='navbar-toggle margin-left-10px' data-toggle="collapse" data-target="#navigation-etiquette-collapse"> <!-- Set the data-target equal to the ID name of the div that holds the class of 'navbar-collapse' so that the main navigation menu doesn't collapse as well. #etiquette-xs-button only serves for selecting this button so the toggle of main navigation isn't selected. -->
        <span class='sr-only'>Toggle navigation</span>
        <span class='icon-bar'></span>
        <span class='icon-bar'></span>
        <span class='icon-bar'></span>
        </button>
        <div class="navbar-brand">Etiquette shortcut menu:</div>
</div><!-- END '#navbar-header' -->
<div id='navigation-etiquette-collapse' class='collapse navbar-collapse'><!-- ID "navigation-etiquette-collapse' is referenced only for data-target for <button data-toggle="collapse" -->
    <ul class='nav navbar-nav' itemscope itemtype="http://www.schema.org/SiteNavigationElement">
       <li class='dropdown xs-fix-menu'><a href="#" class='dropdown-toggle' data-toggle="dropdown">Etiquette Concepts: (First half) <b class='caret'></b></a>
          <ul class='dropdown-menu'><!-- Don't forget that this <ul> needs class='dropdown-menu' to make it a dropdown menu for mobile -->
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#header-container"><span>Introduction (back to top)</span></a></li><!-- #header-container is inside file: includes/header-start.php -->
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#general-concepts"><span>General Concepts</span></a></li><!-- Target: #systems-collapse --><!-- Black-Cat-Kenpo -->
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#becoming-a-student"><span>Becoming a Student</span></a></li>
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#outside-the-martialarts-school"><span>Outside the martial arts school</span></a></li>
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#etiquette-and-safety"><span></span>Etiquette when entering school</a></li>
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#etiquette-in-the-reception-area"><span>Etiquette in reception area</span></a></li>
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#uniform-and-belt"><span>Uniform and Belt</span></a></li>
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#advice-from-the-master"><span>Advice from the Master</span></a></li>
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#before-the-class-starts"><span>Before the class starts</span></a></li>
             <li itemprop="name"><a onclick="toggleMobileHide()" itemprop="url" href="#during-class"><span>During class & requirements</span></a></li>              
          </ul>

此jQuery脚本可隐藏Bootstrap 3的导航菜单,但它在jQuery中。它的转换为纯JavaScript?

<script>    
    $(document).ready(function () {
    $('.dropdown-menu a').click(function(event) {
            $('#etiquette-xs-button.navbar-toggle').click();          
        });       
    });
</script>

我尝试过[下面],但它没有用。显然,有一些事件听众(我认为(阻止我只是添加和删除课程。.hide =显示:无;和.collapse.in = display:block;

<script>
    function toggleMobileHide() {
    var element = document.getElementbyId('etiquette-xs-button');
            element.classList.toggle('collapse', 'navbar-toggle');
    }
</script>

使用新的ES6语法使用纯JavaScript:

$(document).ready(function () {
    $('.dropdown-menu a').click(function(event) {
            $('#etiquette-xs-button.navbar-toggle').click();          
        });       
    });
});

将转化为:

const btns = document.querySelectorAll('.dropdown-menu a');
const elem = document.getElementbyId('etiquette-xs-button');
function toggleDiv() {
    elem.classList.toggle('collapse', 'navbar-toggle');
}
btns.forEach(btn => btn.addEventListener('click', toggleDiv));

但是,如果您更喜欢没有ES6语法的JavaScript(也许是IE兼容(,则:

var btns = document.querySelectorAll('.dropdown-menu a');
var elem = document.getElementbyId('etiquette-xs-button');
function toggleDiv() {
    elem.classList.toggle('collapse', 'navbar-toggle');
}
btns.forEach(
    function (btn){
        btn.addEventListener('click', function () {
            //toggled twice because IE cant handle a *toggle* with more than one parameter
            elem.classList.toggle('collapse');
            elem.classList.toggle('navbar-toggle');
        });
    }

注意:如果您使用的是上面的两个摘要中的任何一个,则无需再添加Inline onClick,从而为您提供清洁的HTML文件。只需确保将脚本放在身体内容之后(例如,在页脚上(即可。

相关内容

  • 没有找到相关文章

最新更新