回答編集履歴

2

コード追記

2021/01/25 23:57

投稿

hatena19
hatena19

スコア33722

test CHANGED
@@ -29,3 +29,15 @@
29
29
  $(".name > li:only-child").parent().addClass("none");
30
30
 
31
31
  ```
32
+
33
+
34
+
35
+ :has() を使うとよりシンプルになりますね。
36
+
37
+
38
+
39
+ ```js
40
+
41
+ $(".name:has(li:only-child)").addClass("none");
42
+
43
+ ```

1

コード追記

2021/01/25 23:57

投稿

hatena19
hatena19

スコア33722

test CHANGED
@@ -1,4 +1,26 @@
1
+ 複数の要素に対して、それぞれに処理をするときは、each をつかうといいでしょう。
2
+
3
+
4
+
5
+ ```js
6
+
7
+ $(".name").each(function(index, element) {
8
+
9
+ const len = $(element).children("li").length;
10
+
1
- 下記でどうでしょう。
11
+ if(len === 1) {
12
+
13
+ $(element).addClass('none');
14
+
15
+ }
16
+
17
+ })
18
+
19
+ ```
20
+
21
+
22
+
23
+ cssセレクタに、`:only-child` というのがあるので、それを使うと下記のようにシンプルに記述できます。
2
24
 
3
25
 
4
26