teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

調整

2019/01/23 07:07

投稿

yambejp
yambejp

スコア117912

answer CHANGED
@@ -11,4 +11,26 @@
11
11
  <a href="#" class="apple orange">both</a>
12
12
  <a href="#" class="">not set</a>
13
13
 
14
- ```
14
+ ```
15
+ # 調整
16
+ ```javascript
17
+ <script>
18
+ window.addEventListener('DOMContentLoaded', function(e){
19
+ [].forEach.call(document.querySelectorAll('a:not(.apple):not(.orange)'),function(x){
20
+ x.addEventListener('click', function(e) {
21
+ if(e.currentTarget.href === window.location.href) {
22
+ alert("oops");
23
+ e.preventDefault();
24
+ e.stopPropagation();
25
+ }
26
+ });
27
+ });
28
+ });
29
+ </script>
30
+ <a href="#apple" class="apple">apple</a>
31
+ <a href="#orange" class="orange">orange</a>
32
+ <a href="#both" class="apple orange">both</a>
33
+ <a href="#none" class="">not set</a>
34
+ ```
35
+ not setを2度押しするとoopsが表示されます
36
+ しかしすでに1回めのクリックで#noneに飛んでいるのでe.preventDefault()する意味がないような?