简体   繁体   English

无法理解classie.js API

[英]having trouble understanding classie.js API

Hello all I'm working through an example of a slide and push menu on another site and am struggling to understand an if statement in the classie.js file that is included with the source download. 大家好,我正在研究另一个网站上的幻灯片和推送菜单示例,并且正在努力理解源下载中随附的classie.js文件中的if语句。

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

I can't seem to wrap my head around the first IF statement. 我似乎无法绕过第一个IF语句。 My understanding is that document.documentElement returns the <html> tag, but what is that 'classList' business? 我的理解是document.documentElement返回<html>标记,但是'classList'业务是什么? The only thing I can find on it was on mdn , but I don't understand at all what that IF statement is testing for. 我只能在mdn上找到它,但是我根本不理解该IF语句正在测试什么。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

Newer browsers support a .classList property on DOM element nodes. 较新的浏览器在DOM元素节点上支持.classList属性。 It's an array of class names. 它是一个类名数组。 Older browsers just support the .className property, which is a single string containing space-separated class names. 较早的浏览器仅支持.className属性,该属性是包含空格分隔的类名的单个字符串。

If the if statement in the code you posted sees that the document node has a .classList property, it knows that it's running on a newer browser. 如果您发布的代码中的if语句发现文档节点具有.classList属性,则表明该节点正在更新的浏览器上运行。 If not, then it creates alternative versions of some class management utilities. 如果不是,那么它将创建某些类管理实用程序的替代版本。

'classList' in document.documentElement simply checks for the existence of classList within the document.documentElement Element. 'classList' in document.documentElement仅检查document.documentElement元素中是否存在classList

From MDN's documentation on the in operator . MDN关于in运算符的文档in

The in operator returns true if the specified property is in the specified object. 如果指定的属性在指定的对象中,则in运算符将返回true。

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

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