【问题标题】:How to use JavaScript to add IDs to multiple elements?如何使用 JavaScript 为多个元素添加 ID?
【发布时间】:2015-08-03 11:38:37
【问题描述】:

我似乎无法解决我的问题。经过几天的深入研究,我得出结论,这个堆栈溢出问题很相似,可能是一个解决方案 (GetElementByID - Multiple IDs),但经过我所有的试验,我似乎仍然无法为我的代码实现解决方案。 [DEMO]。我已经多次问过同样的问题,但我仍然不断得到广泛的答案,这些答案根本没有帮助。 我的问题是,如何从脚本创建多个 ID 来执行多个开放式转换?

 /*!
  * classie - class helper functions
  * classie.has( elem, 'my-class' ) -> true/false
  * classie.add( elem, 'my-new-class' )
  * classie.remove( elem, 'my-unwanted-class' )
  * classie.toggle( elem, 'my-class' )
  */

 /*jshint browser: true, strict: true, undef: true */
 /*global define: false */

 (function (window) {

     'use strict';

     // class helper functions from bonzo https://github.com/ded/bonzo

     function classReg(className) {
         return new RegExp("(^|\\s+)" + className + "(\\s+|$)");
     }

     // classList support for class management
     // altho to be fair, the api sucks because it won't accept multiple classes at once
     var hasClass, addClass, removeClass;

     if ('classList' in document.documentElement) {
         hasClass = function (elem, c) {
             return elem.classList.contains(c);
         };
         addClass = function (elem, c) {
             elem.classList.add(c);
         };
         removeClass = function (elem, c) {
             elem.classList.remove(c);
         };
     } else {
         hasClass = function (elem, c) {
             return classReg(c).test(elem.className);
         };
         addClass = function (elem, c) {
             if (!hasClass(elem, c)) {
                 elem.className = elem.className + ' ' + c;
             }
         };
         removeClass = function (elem, c) {
             elem.className = elem.className.replace(classReg(c), ' ');
         };
     }

     function toggleClass(elem, c) {
         var fn = hasClass(elem, c) ? removeClass : addClass;
         fn(elem, c);
     }

     var classie = {
         // full names
         hasClass: hasClass,
         addClass: addClass,
         removeClass: removeClass,
         toggleClass: toggleClass,
         // short names
         has: hasClass,
         add: addClass,
         remove: removeClass,
         toggle: toggleClass
     };

     // transport
     if (typeof define === 'function' && define.amd) {
         // AMD
         define(classie);
     } else {
         // browser global
         window.classie = classie;
     }

 })(window);


 /**
  * main.js
  * http://www.codrops.com
  *
  * Licensed under the MIT license.
  * http://www.opensource.org/licenses/mit-license.php
  * 
  * Copyright 2014, Codrops
  * http://www.codrops.com
  */ (function () {

     var bodyEl = document.body,
         content = document.querySelector('.content-wrap'),
         openbtn = document.getElementById('open-button'),
         closebtn = document.getElementById('close-button'),
         isOpen = false;

     function init() {
         initEvents();
     }

     function initEvents() {
         openbtn.addEventListener('click', toggleMenu);
         if (closebtn) {
             closebtn.addEventListener('click', toggleMenu);
         }

         // close the menu element if the target it´s not the menu element or one of its descendants..
         content.addEventListener('click', function (ev) {
             var target = ev.target;
             if (isOpen && target !== openbtn) {
                 toggleMenu();
             }
         });
     }

     function toggleMenu() {
         if (isOpen) {
             classie.remove(bodyEl, 'show-menu');
         } else {
             classie.add(bodyEl, 'show-menu');
         }
         isOpen = !isOpen;
     }

     init();

 })();

虽然我特别想通了。

 var bodyEl = document.body,
         content = document.querySelector('.content-wrap'),
         openbtn = document.getElementById('open-button'),
         closebtn = document.getElementById('close-button'),
         isOpen = false;

如果您滚动到结果屏幕的中间,它们是两个按钮,显示为三个灰点。第一组灰点将打开两个黑框。 我不想这样。我会希望第一组灰点打开第一个框,第二组灰点打开第二个框,等等。

我认为这部分 javascript 是为了实现这一点需要进行更改,尽管就像我之前所说的那样,我似乎无法实现该解决方案。一些非常聪明的头脑可以帮我解决这个问题!请不要建议!只是解决问题的直截了当的答案!

有人可以帮帮我吗,我看到用户用代码段落回答,我的问题对于经验丰富的人来说一点也不难,我知道之前已经完成了!!。

【问题讨论】:

  • 您的问题标题和正文问的是两个不同的问题;是哪一个?你想知道如何使用Document.getElementsByClassName,还是想使用JavaScript为多个元素添加ID?
  • 任何一种方法都可以正常工作,但更可能是如何使用 Javascript 将 ID 添加到多个元素。
  • 我认为如果您描述需要解决方案的问题而不是粘贴我们需要阅读的脚本以尝试了解您要解决的问题,我认为这个问题可以得到改善。
  • 我在最后解释了这个问题。如果您滚动到结果屏幕的中间,它们是两个按钮,显示为三个灰点。第一组灰点将打开两个黑框。我不要那个。我只希望第一组灰点只打开第一个框,第二组灰点只打开第二个框,依此类推。
  • 对别人大喊大叫不会帮助你实现目标。此外,如果您希望有人提供帮助,您应该听取提供给您的建议。

标签: javascript html css


【解决方案1】:

没有人愿意为您编写代码。这是一个问答网站,而不是“为我修复我的代码”网站。对大写粗体的人大喊大叫不会得到你需要的东西。

尽管已经说过...您有两个具有相同 ID 的按钮,它们试图“打开”没有 ID 的不同元素。 ID 应该是不同的,因此为您的按钮提供不同的 ID 并为其附加不同的 click 函数,或者使用相同的方法但传入元素的 ID 以进行切换。 click 函数应该修改特定菜单包装元素的类,由其自己的不同 ID 定义。

另一个问题是您的 CSS 代码修改了正文的类以打开菜单。您可能应该通过修改您需要显示/隐藏的特定元素的类。

Here 是您的代码的修改版本。它有点工作,但应该让你更好地了解你需要做什么。您还应该考虑清理一些代码,并可能考虑使用 jQuery,因为它对新开发人员更好。

【讨论】:

    【解决方案2】:

    您正在使用.show-menu .menu-wrap{...} 打开 div,当您单击灰点时,您将类 .show-menu 设置为 body,因此两个 div 都受到样式 .show-menu .menu-wrap 的影响,因为两者都在 body 内。

    修复它的方法是在它们周围放置一个包装器并将.show-menu 切换到您单击的灰点元素的父级,如下所示:

    $(".menu-button").click(function () {
        $(this).parents(".wrapper").toggleClass("show-menu");
    });
    body, .container, .content-wrap {
        width: 100%;
        height: 100%;
    }
    .container {
        background: #fff;
    }
    .menu-wrap a {
        color: #b8b7ad;
    }
    .menu-wrap a:hover, .menu-wrap a:focus {
        color: #c94e50;
    }
    .content-wrap {
        overflow-y: scroll;
        -webkit-overflow-scrolling: touch;
    }
    .content {
        position: absolute;
        background: #fff;
        overflow-y: visible;
    }
    .content::before {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 10;
        width: 100%;
        height: 100%;
        background: none;
        content:'';
        opacity: 0;
        -webkit-transform: translate3d(100%, 0, 0);
        transform: translate3d(100%, 0, 0);
        -webkit-transition: opacity 0.4s, -webkit-transform 0s 0.4s;
        transition: opacity 0.4s, transform 0s 0.4s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    /* Menu Button 1 */
     .menu-button {
        position: absolute;
        z-index: 1000;
        margin: 1em;
        padding: 0;
        width: 10px;
        height: 50px;
        border: none;
        text-indent: 2.5em;
        font-size: 1.5em;
        color: transparent;
        background: transparent;
        opacity: 1;
        top: 510px;
        -ms-transform: rotate(7deg);
        /* IE 9 */
        -webkit-transform: rotate(7deg);
        /* Chrome, Safari, Opera */
        transform: rotate(90deg);
        cursor: pointer;
    }
    .menu-button::before {
        position: absolute;
        top: 0.5em;
        right: 0.2em;
        bottom: 0.4em;
        left: -1px;
        background: linear-gradient(#929292 20%, transparent 20%, transparent 40%, #929292 40%, #929292 58%, transparent 0%, transparent 80%, #929292 80%);
        content:'';
    }
    .menu-button:hover {
        opacity: 1;
    }
    /* Close Button */
     .close-button {
        width: 50px;
        height: 50px;
        position: absolute;
        right: 1em;
        top: 1em;
        overflow: hidden;
        text-indent: 1em;
        font-size: 0.75em;
        border: none;
        background: transparent;
        color: transparent;
        opacity: 0;
    }
    .close-button::before, .close-button::after {
        content:'';
        position: absolute;
        width: 3px;
        height: 100%;
        top: 0;
        left: 50%;
        background: #bdc3c7;
    }
    .close-button::before {
        -webkit-transform: rotate(45deg);
        transform: rotate(45deg);
    }
    .close-button::after {
        -webkit-transform: rotate(-45deg);
        transform: rotate(-45deg);
    }
    /* Comments */
     .menu-wrap {
        position: absolute;
        z-index: 1000;
        width: 429.0500011444092px;
        height: 600.875px;
        right: 0;
        background: #0C0C0C;
        top: 6px;
        padding: 0;
        font-size: 1.15em;
        -webkit-transform: translate3d(500px, 0, 0);
        transform: translate3d(500px, 0, 0);
        -webkit-transition: -webkit-transform 0.4s;
        transition: transform 0.4s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    .menu, .icon-list {
        height: 100%;
    }
    .icon-list {
        -webkit-transform: translate3d(0, 100%, 0);
        transform: translate3d(0, 100%, 0);
    }
    .icon-list a {
        display: block;
        padding: 0.8em;
        -webkit-transform: translate3d(0, 500px, 0);
        transform: translate3d(0, 500px, 0);
    }
    .icon-list, .icon-list a {
        -webkit-transition: -webkit-transform 0s 0.4s;
        transition: transform 0s 0.4s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    .icon-list a:nth-child(2) {
        -webkit-transform: translate3d(0, 1000px, 0);
        transform: translate3d(0, 1000px, 0);
    }
    .icon-list a:nth-child(3) {
        -webkit-transform: translate3d(0, 1500px, 0);
        transform: translate3d(0, 1500px, 0);
    }
    .icon-list a:nth-child(4) {
        -webkit-transform: translate3d(0, 2000px, 0);
        transform: translate3d(0, 2000px, 0);
    }
    .icon-list a:nth-child(5) {
        -webkit-transform: translate3d(0, 2500px, 0);
        transform: translate3d(0, 2500px, 0);
    }
    .icon-list a:nth-child(6) {
        -webkit-transform: translate3d(0, 3000px, 0);
        transform: translate3d(0, 3000px, 0);
    }
    .icon-list a span {
        margin-left: 10px;
        font-weight: 700;
    }
    /* Shown menu */
     .show-menu .menu-wrap {
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
        -webkit-transition: -webkit-transform 0.8s;
        transition: transform 0.8s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    .show-menu .icon-list, .show-menu .icon-list a {
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
        -webkit-transition: -webkit-transform 0.8s;
        transition: transform 0.8s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    .show-menu .icon-list a {
        -webkit-transition-duration: 0.9s;
        transition-duration: 0.9s;
    }
    .show-menu .content::before {
        opacity: 1;
        -webkit-transition: opacity 0.8s;
        transition: opacity 0.8s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <body>
        <div class="wrapper">
            <button class="menu-button">Open Menu</button>
            <div class="menu-wrap"></div>
        </div>
        <div class="wrapper">
            <button class="menu-button" style="
        top: 560px;
    ">Open Menu</button>
            <div class="menu-wrap" style="
        top: 700px;
    "></div>
        </div>
    </body>

    或者,如果您不想更改标记,只需将类 .show-menu 切换到 .menu-wrap 本身并将选择器更改为 .show-menu.menu-wrap{...},如下所示:

    $(".menu-button").click(function () {
        $(this).next(".menu-wrap").toggleClass("show-menu");
    });
    body, .container, .content-wrap {
        width: 100%;
        height: 100%;
    }
    .container {
        background: #fff;
    }
    .menu-wrap a {
        color: #b8b7ad;
    }
    .menu-wrap a:hover, .menu-wrap a:focus {
        color: #c94e50;
    }
    .content-wrap {
        overflow-y: scroll;
        -webkit-overflow-scrolling: touch;
    }
    .content {
        position: absolute;
        background: #fff;
        overflow-y: visible;
    }
    .content::before {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 10;
        width: 100%;
        height: 100%;
        background: none;
        content:'';
        opacity: 0;
        -webkit-transform: translate3d(100%, 0, 0);
        transform: translate3d(100%, 0, 0);
        -webkit-transition: opacity 0.4s, -webkit-transform 0s 0.4s;
        transition: opacity 0.4s, transform 0s 0.4s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    /* Menu Button 1 */
     .menu-button {
        position: absolute;
        z-index: 1000;
        margin: 1em;
        padding: 0;
        width: 10px;
        height: 50px;
        border: none;
        text-indent: 2.5em;
        font-size: 1.5em;
        color: transparent;
        background: transparent;
        opacity: 1;
        top: 510px;
        -ms-transform: rotate(7deg);
        /* IE 9 */
        -webkit-transform: rotate(7deg);
        /* Chrome, Safari, Opera */
        transform: rotate(90deg);   
        cursor: pointer;
    }
    .menu-button::before {
        position: absolute;
        top: 0.5em;
        right: 0.2em;
        bottom: 0.4em;
        left: -1px;
        background: linear-gradient(#929292 20%, transparent 20%, transparent 40%, #929292 40%, #929292 58%, transparent 0%, transparent 80%, #929292 80%);
        content:'';
    }
    .menu-button:hover {
        opacity: 1;
    }
    /* Close Button */
     .close-button {
        width: 50px;
        height: 50px;
        position: absolute;
        right: 1em;
        top: 1em;
        overflow: hidden;
        text-indent: 1em;
        font-size: 0.75em;
        border: none;
        background: transparent;
        color: transparent;
        opacity: 0;
    }
    .close-button::before, .close-button::after {
        content:'';
        position: absolute;
        width: 3px;
        height: 100%;
        top: 0;
        left: 50%;
        background: #bdc3c7;
    }
    .close-button::before {
        -webkit-transform: rotate(45deg);
        transform: rotate(45deg);
    }
    .close-button::after {
        -webkit-transform: rotate(-45deg);
        transform: rotate(-45deg);
    }
    /* Comments */
     .menu-wrap {
        position: absolute;
        z-index: 1000;
        width: 429.0500011444092px;
        height: 600.875px;
        right: 0;
        background: #0C0C0C;
        top: 6px;
        padding: 0;
        font-size: 1.15em;
        -webkit-transform: translate3d(500px, 0, 0);
        transform: translate3d(500px, 0, 0);
        -webkit-transition: -webkit-transform 0.4s;
        transition: transform 0.4s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    .menu, .icon-list {
        height: 100%;
    }
    .icon-list {
        -webkit-transform: translate3d(0, 100%, 0);
        transform: translate3d(0, 100%, 0);
    }
    .icon-list a {
        display: block;
        padding: 0.8em;
        -webkit-transform: translate3d(0, 500px, 0);
        transform: translate3d(0, 500px, 0);
    }
    .icon-list, .icon-list a {
        -webkit-transition: -webkit-transform 0s 0.4s;
        transition: transform 0s 0.4s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    .icon-list a:nth-child(2) {
        -webkit-transform: translate3d(0, 1000px, 0);
        transform: translate3d(0, 1000px, 0);
    }
    .icon-list a:nth-child(3) {
        -webkit-transform: translate3d(0, 1500px, 0);
        transform: translate3d(0, 1500px, 0);
    }
    .icon-list a:nth-child(4) {
        -webkit-transform: translate3d(0, 2000px, 0);
        transform: translate3d(0, 2000px, 0);
    }
    .icon-list a:nth-child(5) {
        -webkit-transform: translate3d(0, 2500px, 0);
        transform: translate3d(0, 2500px, 0);
    }
    .icon-list a:nth-child(6) {
        -webkit-transform: translate3d(0, 3000px, 0);
        transform: translate3d(0, 3000px, 0);
    }
    .icon-list a span {
        margin-left: 10px;
        font-weight: 700;
    }
    /* Shown menu */
     .show-menu.menu-wrap {
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
        -webkit-transition: -webkit-transform 0.8s;
        transition: transform 0.8s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    .show-menu .icon-list, .show-menu .icon-list a {
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
        -webkit-transition: -webkit-transform 0.8s;
        transition: transform 0.8s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
    }
    .show-menu .icon-list a {
        -webkit-transition-duration: 0.9s;
        transition-duration: 0.9s;
    }
    .show-menu .content::before {
        opacity: 1;
        -webkit-transition: opacity 0.8s;
        transition: opacity 0.8s;
        -webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <body>
        <button class="menu-button">Open Menu</button>
        <div class="menu-wrap"></div>
        <button class="menu-button" style="
        top: 560px;
    ">Open Menu</button>
        <div class="menu-wrap" style="
        top: 700px;
    "></div>
    </body>

    上面的例子只是给你一个大概的思路,作​​业是用纯javascript做的,但我真的推荐你用jquery。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 2016-03-31
      • 2019-04-15
      • 1970-01-01
      • 2021-05-05
      • 2013-01-23
      相关资源
      最近更新 更多