當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML ondblclick事件用法及代碼示例


HTML DOM ondblclick事件在用戶雙擊時發生。

除ondblclick事件外,所有HTML元素均受支持:

  • <bdo>
  • <br>
  • <base>
  • <head>
  • <html>
  • <iframe>
  • <meta>
  • <param>
  • <script>
  • <style>
  • <title>.

用法:
在HTML中:


<element ondblclick="myScript">

在JavaScript中:

object.ondblclick = function(){myScript};

在JavaScript中,使用addEventListener()方法:

object.addEventListener("dblclick", myScript);

範例1:使用HTML

<!DOCTYPE html> 
<html> 
  <head> 
    <title>HTML DOM ondblclick Event</title> 
  </head> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
        <h2>HTML DOM ondblclick Event</h2> 
        <p id="demo"
           ondblclick="myFunction()"> 
            Double-click 
        </p> 
  
        <script> 
            function myFunction() { 
                document.getElementById( 
                    "demo").innerHTML = "GeeksforGeeks"; 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:
之前:

後:

例:使用JavaScript

<!DOCTYPE html> 
<html> 
<head> 
    <title>HTML DOM ondblclick Event</title> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
        <h2>HTML DOM ondblclick Event</h2> 
        <p id="demo">Double-click me.</p> 
  
        <script> 
            document.getElementById( 
              "demo").ondblclick = function() { 
                GFGfun() 
            }; 
  
            function GFGfun() { 
                document.getElementById( 
                  "demo").innerHTML = "GeeksforGeeks"; 
            } 
        </script> 
    </center> 
  
</body> 
  
</html>

輸出:
之前:

後:

例:在JavaScript中,使用addEventListener()方法:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>HTML DOM ondblclick Event</title> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
        <h2>HTML DOM ondblclick Event</h2> 
        <p id="demo">Double-click me.</p> 
  
        <script> 
            document.getElementById( 
              "demo").addEventListener( 
              "dblclick", GFGfun); 
  
            function GFGfun() { 
                document.getElementById( 
                  "demo").innerHTML = "GeeksforGeeks"; 
            } 
            
        </script> 
    </center> 
</body> 
  
</html>

輸出:
之前:

後:

支持的瀏覽器:下麵列出了ondblclick事件支持的瀏覽器:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera


相關用法


注:本文由純淨天空篩選整理自Vijay Sirra大神的英文原創作品 HTML | DOM ondblclick Event。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。