回答編集履歴

1

追記

2022/01/03 14:26

投稿

umau
umau

スコア831

test CHANGED
@@ -31,3 +31,77 @@
31
31
  </script>
32
32
 
33
33
  ```
34
+
35
+
36
+
37
+ ---
38
+
39
+ (追記)
40
+
41
+ 動作確認したソース丸ごと貼っておきます。
42
+
43
+ ```html
44
+
45
+ <!DOCTYPE html>
46
+
47
+ <html lang="en">
48
+
49
+ <head>
50
+
51
+ <meta charset="UTF-8">
52
+
53
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
54
+
55
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
56
+
57
+ <title>Document</title>
58
+
59
+ </head>
60
+
61
+ <body>
62
+
63
+ <div class="Recommend1">
64
+
65
+ <h3 id="Recommend1">〇〇〇〇</h3>
66
+
67
+ <div id="result1"></div>
68
+
69
+ <input type="button" value="くじを引く" id="bt1"/>
70
+
71
+ <script>
72
+
73
+ var target = document.getElementById( 'Recommend1' );
74
+
75
+ var bt = document.getElementById( 'bt1' );
76
+
77
+ bt.addEventListener( 'click', function( e ) {
78
+
79
+ var imglist = [
80
+
81
+ ['images1.jpg', 'https://www.yahoo.co.jp/'],
82
+
83
+ ['images2.jpg', 'https://www.youtube.com/']
84
+
85
+ ]
86
+
87
+
88
+
89
+ var selectnum = Math.floor(Math.random() * imglist.length);
90
+
91
+ var item = imglist[selectnum]
92
+
93
+ var element = '<a href="' + item[1] +'"><img src="' + item[0] + '" alt="" /></a>';
94
+
95
+ result1.innerHTML = element;
96
+
97
+ }, false );
98
+
99
+ </script>
100
+
101
+ </div>
102
+
103
+ </body>
104
+
105
+ </html>
106
+
107
+ ```