Check if class already assigned before adding

后端 未结 4 1758
面向向阳花
面向向阳花 2020-12-01 05:47

In jQuery, is it recommended to check if a class is already assigned to an element before adding that class? Will it even have any effect at all?

For example:

<
4条回答
  •  猫巷女王i
    2020-12-01 06:22

    A simple check in the console would have told you that calling addClass multiple times with the same class is safe.

    Specifically you can find the check in the source

    if ( !~setClass.indexOf( " " + classNames[ c ] + " " ) ) {
      setClass += classNames[ c ] + " ";
    }
    

提交回复
热议问题