最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use duplicate class name on an element? - Stack Overflow

programmeradmin0浏览0评论

I found that there are many frameworks that will check the duplicate class name before adding the new class name on the element which I think will slow down the performance.

Are there any problems when the element has a duplicate class name?

It will also apply the CSS class without conflict while the duplicate class name is in use.

<div class="aa bb cc aa"></div>

Is it OK to add a class name simply just like elem.className += ' ' + 'aa ee', even if the element has a duplicate class name?

I found that there are many frameworks that will check the duplicate class name before adding the new class name on the element which I think will slow down the performance.

Are there any problems when the element has a duplicate class name?

It will also apply the CSS class without conflict while the duplicate class name is in use.

<div class="aa bb cc aa"></div>

Is it OK to add a class name simply just like elem.className += ' ' + 'aa ee', even if the element has a duplicate class name?

Share Improve this question edited Jan 1, 2023 at 14:03 Angolao asked Apr 7, 2012 at 1:07 AngolaoAngolao 9923 gold badges15 silver badges27 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

There is nothing "wrong" with having a duplicate class name, it's just redundant. There's probably a small performance impact, but that'll only really make much difference if you have a lot of duplication.

Also, preventing duplicates just helps to keep things tidy.

Semantic UI heavily uses attribute selectors

like here

.ui.grid [class*="left floated"].column {
  margin-right: auto;
}
.ui.grid [class*="right floated"].column {
  margin-left: auto;
}

If you want margin-left and margin-right to have the auto value, you must have a duplicate of the classname segment floated (ex. <div class="left floated right floated">Lorem</div>)

The DOM-API provides a convenient way for adding and removing and testing class-names of DOM-elements: classList.add(name) and classList.remove(name) and classList.contains(name).

In the ES6 context the question could perhaps be rephrased as:

"If I use .classList.add() to add the same className more than once, and then want to remove that class with classList.remove(), do I need to call classList.remove() multiple times?

Luckily the answer seems to be a single call to .classList.remove() is enough to remove a given class no matter how many times you have added it.

I couldn't find an easy answer to this by Google so I wrote the following test to tell me that is how it behaves. Calling the below function with DOM-element as argument does NOT throw an error:

function testClassListRemove (dem)
{
  dem.classList.add ("hello");
  dem.classList.add ("hello");
  dem.classList.add ("hello");
  ok (dem.classList.contains ("hello") );

  dem.classList.remove ("hello");
  ok (!  dem.classList.contains ("hello") );

  function ok (b)
  { if (! b)
    { throw new Error ('not ok ');
    }
  }
}
发布评论

评论列表(0)

  1. 暂无评论