简体   繁体   English

在JS字符串中定位多个类?

[英]Target multiple classes in JS string?

I'm trying to target two classes in the same JS snippet. 我正在尝试在同一JS代码段中定位两个类。 I know 0 about JS. 我对JS的了解为0。 Is there an easy way to try and get at two classes with one JS effect? 有没有一种简单的方法可以尝试使用一种JS效果获得两个类? It seems to break when I use comma delimiters. 当我使用逗号分隔符时,它似乎断开了。

trigger-s-overlay & trigger-a-overlay 触发S叠加和触发A叠加

My JS: 我的JS:

/* Search */
(function() {
  var triggerBttn = document.getElementById( 'trigger-s-overlay' ),
    overlay = document.querySelector( 'div.s-overlay' ),
    closeBttn = overlay.querySelector( 'button.overlay-close' );
    transEndEventNames = {
      'WebkitTransition': 'webkitTransitionEnd',
      'MozTransition': 'transitionend',
      'OTransition': 'oTransitionEnd',
      'msTransition': 'MSTransitionEnd',
      'transition': 'transitionend'
    },
    transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
    support = { transitions : Modernizr.csstransitions };

  function toggleOverlay() {
    if( classie.has( overlay, 'open' ) ) {
      classie.remove( overlay, 'open' );
      classie.add( overlay, 'close' );
      var onEndTransitionFn = function( ev ) {
        if( support.transitions ) {
          if( ev.propertyName !== 'visibility' ) return;
          this.removeEventListener( transEndEventName, onEndTransitionFn );
        }
        classie.remove( overlay, 'close' );
      };
      if( support.transitions ) {
        overlay.addEventListener( transEndEventName, onEndTransitionFn );
      }
      else {
        onEndTransitionFn();
      }
    }
    else if( !classie.has( overlay, 'close' ) ) {
      classie.add( overlay, 'open' );
    }
  }

  triggerBttn.addEventListener( 'click', toggleOverlay );
  closeBttn.addEventListener( 'click', toggleOverlay );
})();

My HTML: 我的HTML:

<a id="trigger-s-overlay" class="global_btn icon_btn"><span class="icon-search"></span></a>

<div class="s-overlay overlay-scale search_overlay">
   content
</div>

&

<a id="trigger-a-overlay" class="global_btn icon_btn"><span class="icon-search"></span></a>

<div class="a-overlay overlay-scale">
    content
</div>

getElementById() returns one element only. getElementById()仅返回一个元素。 It cannot return multiple elements. 它不能返回多个元素。

There are other related functions that can return an array of elements, like getElementsByClassName or getElementsByName . 还有其他一些可以返回元素数组的相关函数,例如getElementsByClassNamegetElementsByName Maybe they can help you solving your needs. 也许他们可以帮助您解决您的需求。 But note that some of them may not work in every browser. 但是请注意,其中某些功能可能无法在每种浏览器中都起作用。

If you have to interact with the DOM often, JQuery is a library that can help you with this. 如果您必须经常与DOM交互, JQuery是一个可以帮助您解决此问题的库。 It makes interacting with the DOM quite easy and has lots of functionality and flexibility. 它使与DOM的交互变得非常容易,并且具有许多功能和灵活性。

It looks like you have two sets of elements that relate to one another, and the only difference is s rather than a in the id of the trigger button and the class of the overlay. 它看起来像你有两套涉及到另一个元素,而唯一的区别是s ,而不是aid触发按钮和类覆盖的。

You can move your code into a function, pass the function a type ( s or a ), and have it use that type when setting things up. 您可以将代码移动到函数中,将函数传递给类型( sa ),并在设置时使用该类型。 Something along these lines (see comments, you'll have to scroll right for a couple of them): 这些内容(请参见注释,您必须向右滚动其中的几个内容):

(function() {
  // Added these:
  setup("s");
  setup("a");

  // Added the function wrapper
  function setup(type) {
      var triggerBttn = document.getElementById( 'trigger-' + type + '-overlay' ), // <== Changed
        overlay = document.querySelector( 'div.' + type + '-overlay' ),            // <== Changed
        closeBttn = overlay.querySelector( 'button.overlay-close' );
        transEndEventNames = {
          'WebkitTransition': 'webkitTransitionEnd',
          'MozTransition': 'transitionend',
          'OTransition': 'oTransitionEnd',
          'msTransition': 'MSTransitionEnd',
          'transition': 'transitionend'
        },
        transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
        support = { transitions : Modernizr.csstransitions };

      function toggleOverlay() {
        if( classie.has( overlay, 'open' ) ) {
          classie.remove( overlay, 'open' );
          classie.add( overlay, 'close' );
          var onEndTransitionFn = function( ev ) {
            if( support.transitions ) {
              if( ev.propertyName !== 'visibility' ) return;
              this.removeEventListener( transEndEventName, onEndTransitionFn );
            }
            classie.remove( overlay, 'close' );
          };
          if( support.transitions ) {
            overlay.addEventListener( transEndEventName, onEndTransitionFn );
          }
          else {
            onEndTransitionFn();
          }
        }
        else if( !classie.has( overlay, 'close' ) ) {
          classie.add( overlay, 'open' );
        }
      }

      triggerBttn.addEventListener( 'click', toggleOverlay );
      closeBttn.addEventListener( 'click', toggleOverlay );
  }
})();

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

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