AddClass adds the given class(es) to each element in the set of matched elements. Multiple class names can be specified, separated by a space or via multiple arguments.
(class ...string)
| 108 | // AddClass adds the given class(es) to each element in the set of matched elements. |
| 109 | // Multiple class names can be specified, separated by a space or via multiple arguments. |
| 110 | func (s *Selection) AddClass(class ...string) *Selection { |
| 111 | classStr := strings.Trim(strings.Join(class, " "), " \t\r\n\f") |
| 112 | |
| 113 | if classStr == "" { |
| 114 | return s |
| 115 | } |
| 116 | |
| 117 | tcls := getClassesSlice(classStr) |
| 118 | for _, n := range s.Nodes { |
| 119 | curClasses, attr := getClassesAndAttr(n) |
| 120 | for _, newClass := range tcls { |
| 121 | if !strings.Contains(curClasses, " "+newClass+" ") { |
| 122 | curClasses += newClass + " " |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | setClasses(n, attr, curClasses) |
| 127 | } |
| 128 | |
| 129 | return s |
| 130 | } |
| 131 | |
| 132 | // HasClass determines whether any of the matched elements are assigned the |
| 133 | // given class. |