简体   繁体   English

如何使用javascript保存类列表,以便以后与其他类进行比较?

[英]How to save a list of classes using javascript to compare them against other classes later?

I have a series of polygons : 我有一系列多边形:

<svg>
   <path class="_1600 _1500 _1400 leaflet"></path>
   <path class="_1300 _1700 _1900 leaflet"></path>
   <path class="_1600 _1400 _1800 leaflet"></path>
</svg>

And buttons: 和按钮:

<button class="_1600 _1500"></button>
<button class="_1600 _1300 _1200"></button>
<button class="_1300 _1200 _1700 _1800"></button>    
<button class="_1300 _1200 _1100 _1900"></button>

I need to assign a class to it on a click and this is how I do it and works fine: 我需要为它分配一个类,这是我的操作方式,并且工作正常:

On click: 点击时:

    this.getElement().classList.add("active");

Now the polygons become: 现在,多边形变为:

<svg>
   <path class="_1600 _1500 _1400 leaflet"></path>
   <path class="active _1300 _1700 _1900 leaflet"></path>
   <path class="_1600 _1400 _1800 leaflet"></path>
</svg>

How can I save the list of classes of the clicked element (to which I also have added .active after a click)? 如何保存clicked元素的类列表(单击后我也向其中添加了.active )? I would like to save them so that I can later comprare this list for any match with another list of classes and if any match, attach a .active class to the matching elements. 我想保存它们,以便以后可以将该列表与另一个类列表进行任何比较,如果有匹配项,请将.active类附加到匹配的元素上。

I must use: 我必须使用:

function onEachFeature(feature, layer) {
    layer.on({
        click: panelShow
    });
}

function panelShow(e) {
    $("path").removeClass("active");
    this.getElement().classList.add('active');
}

You can use className to get a space-separated string containing all the classes. 您可以使用className获取包含所有类的空格分隔的字符串。

this.getElement().className;

If you want to have something more fancy, like a static classList copy, you can always create a dummy element: 如果您想拥有更多精美的东西,例如静态classList副本,则可以始终创建一个哑元素:

function staticClassListCopy(element) {
  var dummy = document.createElement('div');
  dummy.className = element.className; // import classes
  return dummy.classList;
}

This object will be an array-like, so you can iterate it easily to do the desired comparisons. 该对象将类似于数组,因此您可以轻松地对其进行迭代以进行所需的比较。

Working code: 工作代码:

var buttons = [].slice.call(document.querySelectorAll('.buttons button'));

function panelShow(e) {
  $("path").removeClass("active");
  var clickedPath = this.getElement();
  clickedPath.classList.add("active");
  var datum = (clickedPath.getAttribute('class').match(/\d+/g) || [])[0];
  buttons.forEach(function(btn) {
    var method = btn.getAttribute('data-date') === datum ? 'add' : 'remove';
    btn.classList[method]('active');
  });
}

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

相关问题 如何在HTML中将类列表与其他类进行比较? - How to compare a list of classes against other classes in html? 如何比较javascript中类的相等性? - How to compare the equality of classes in javascript? 如何将包含连字符/破折号(?)的所有类应用于任何 DOM 元素并将它们保存到 JavaScript 的数组中? - How to get all classes containing hyphen/dash(?) applied to any DOM element and save them into array in JavaScript? ES6中的Javascript类,如何将其导入其他js文件和html文件中? - Javascript classes in ES6, how to import them within other js-files and in html files? 如何在javascript中相互比较两个列表项? - How do I compare two list items against each other in javascript? Javascript使用函数访问其他类 - Javascript accessing other classes using functions 比较2个独立JavaScript对象的类 - Compare the classes of 2 separate JavaScript objects 如何比较数组中的每个数字? (JavaScript的) - How to compare every number in an array against each other? (javascript) 如何访问数组中的类并使用javascript设置样式 - How can I access classes within arrays and style them using javascript 如何在CoffeeScript或Javascript中列出继承的类的方法 - How to list methods of inherited classes in CoffeeScript or Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM