…うーん、、仰っている意味が全然ピンとこないのですが、、、こういうことですかね…?
idよりもclassの方が適しているように感じます。またdocument.write
だと非効率です。
html
1<p class="url_show"></p>
2<p class="url_show"></p>
3<p class="url_show"></p>
4...
js
1Array.prototype.forEach.call(
2 document.getElementsByClassName("url_show"),
3 function(e){
4 e.innerHTML = "URL:"+location.href;
5 }
6);
おまけ。全体のhtmlコード例です。
html
1<!DOCTYPE html><html>
2<head>
3 <meta charset="UTF-8">
4 <title>116874</title>
5</head>
6<body>
7 <p class="url_show"></p>
8 <p class="url_show"></p>
9 <p class="url_show"></p>
10 <p class="url_show"></p>
11
12 <script>
13 Array.prototype.forEach.call(
14 document.getElementsByClassName("url_show"),
15 function(e){
16 e.innerHTML = location.href;
17 }
18 );
19 </script>
20</body>
21</html>
上記コードでは、jsは<p class="url_show"></p>
が全て読み込まれてから実行される必要があるので、<script>
は下に書いています。
addEventListener("load"...
を使う場合などはその限りにはならず、上に書いても実行されます。
html
1<!DOCTYPE html><html>
2<head>
3 <meta charset="UTF-8">
4 <title>116874</title>
5 <script>
6 addEventListener("load",function(){
7 Array.prototype.forEach.call(
8 document.getElementsByClassName("url_show"),
9 function(e){
10 e.innerHTML = location.href;
11 }
12 );
13 });
14 </script>
15</head>
16<body>
17 <p class="url_show"></p>
18 <p class="url_show"></p>
19 <p class="url_show"></p>
20 <p class="url_show"></p>
21</body>
22</html>
23
この辺りは「勉強」だなんてかしこまらずすぐに「addEventListener」や、「js 実行タイミング」等のキーワードでグーグル先生に聞いてみましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/10 03:10
2018/03/10 03:12
2018/03/10 03:23
2018/03/10 04:00 編集
2018/03/10 04:02