回答編集履歴

4

調整

2022/01/27 09:06

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -37,9 +37,9 @@
37
37
  #test3{color:lime;}
38
38
  #test4{color:lime !important;}
39
39
  </style>
40
- <font id="test1" color="red">1</font>
40
+ <font id="test1" color="red">1</font> <!-- color属性を反映して赤くなる -->
41
- <font id="test2" color="red" style="color:blue">2</font>
41
+ <font id="test2" color="red" style="color:blue">2</font> <!-- style属性のcolorを反映して青くなる -->
42
- <font id="test3" color="red" style="color:blue">3</font>
42
+ <font id="test3" color="red" style="color:blue">3</font> <!-- スタイルシートよりstyle属性が勝つので青-->
43
- <font id="test4" color="red" style="color:blue">4</font>
43
+ <font id="test4" color="red" style="color:blue">4</font> <!-- スタイルシートを優先して緑になる -->
44
44
  ```
45
45
  jQueryと生jsとの比較です

3

調整

2022/01/27 09:03

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -1,19 +1,45 @@
1
1
  colorという属性が設定されていないからでは?
2
2
  もちろん変な属性はつけない方がいいです
3
- あえて言えばstyle属性からcolorを見つけることは可能ですが、
4
3
  それをやるのがcssメソッドでしょう
5
4
 
6
5
  ```javascript
7
6
  <script>
8
7
  $(function(){
8
+ console.log([
9
- console.log($('#hoge').attr('color'));
9
+ $('#test1').attr('color'),
10
+ $('#test2').attr('color'),
11
+ $('#test3').attr('color'),
12
+ $('#test4').attr('color'),
13
+ ]);
14
+ console.log([
10
- console.log($('#hoge').css('color'));
15
+ $('#test1').css('color'),
16
+ $('#test2').css('color'),
17
+ $('#test3').css('color'),
18
+ $('#test4').css('color'),
19
+ ]);
20
+ });
21
+ window.addEventListener('DOMContentLoaded', ()=>{
22
+ console.log([
23
+ test1.getAttribute('color'),
24
+ test2.getAttribute('color'),
25
+ test3.getAttribute('color'),
26
+ test4.getAttribute('color'),
27
+ ]);
28
+ console.log([
11
- console.log(getComputedStyle(fuga).color);
29
+ getComputedStyle(test1).color,
30
+ getComputedStyle(test2).color,
31
+ getComputedStyle(test3).color,
32
+ getComputedStyle(test4).color,
33
+ ]);
12
34
  });
13
35
  </script>
14
36
  <style>
15
- #fuga{color:lime;}
37
+ #test3{color:lime;}
38
+ #test4{color:lime !important;}
16
39
  </style>
40
+ <font id="test1" color="red">1</font>
17
- <div id="hoge" color="red" style="color:yellow">hoge</div>
41
+ <font id="test2" color="red" style="color:blue">2</font>
18
- <div id="fuga" >fuga</div>
42
+ <font id="test3" color="red" style="color:blue">3</font>
43
+ <font id="test4" color="red" style="color:blue">4</font>
19
44
  ```
45
+ jQueryと生jsとの比較です

2

調整

2022/01/27 08:39

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -1,3 +1,19 @@
1
1
  colorという属性が設定されていないからでは?
2
2
  もちろん変な属性はつけない方がいいです
3
- あえて言えばstyle属性からcolorを見つけることは可能ですが、それをやるのがcssメソッドでしょう
3
+ あえて言えばstyle属性からcolorを見つけることは可能ですが、
4
+ それをやるのがcssメソッドでしょう
5
+
6
+ ```javascript
7
+ <script>
8
+ $(function(){
9
+ console.log($('#hoge').attr('color'));
10
+ console.log($('#hoge').css('color'));
11
+ console.log(getComputedStyle(fuga).color);
12
+ });
13
+ </script>
14
+ <style>
15
+ #fuga{color:lime;}
16
+ </style>
17
+ <div id="hoge" color="red" style="color:yellow">hoge</div>
18
+ <div id="fuga" >fuga</div>
19
+ ```

1

追記

2022/01/27 08:17

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -1,2 +1,3 @@
1
1
  colorという属性が設定されていないからでは?
2
2
  もちろん変な属性はつけない方がいいです
3
+ あえて言えばstyle属性からcolorを見つけることは可能ですが、それをやるのがcssメソッドでしょう