当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。