简体   繁体   English

ondblclick无法与onclick javascript一起在元素上使用

[英]ondblclick won't work on element with onclick javascript

I have the following problem: I have an image.When the user clicks it it will display an alert box but I also want to remove it if the user double clicks it. 我有以下问题:我有一个图像。当用户单击它时,它将显示一个警报框,但如果用户双击它,我也想删除它。 I have the following markup but it is not working. 我有以下标记,但无法正常工作。

<h1>hello </h1>
            <img src="smile.png" class=" center-block" onclick="alert('Hello')" ondblclick="$('img').remove();">

The only working part is the alert button 唯一的工作部分是警报按钮
Note: jquery is loaded 注意:jquery已加载

From MDN : MDN

Dialog boxes are modal windows - they prevent the user from accessing the rest of the program's interface until the dialog box is closed. 对话框是模式窗口-在对话框关闭之前,它们阻止用户访问程序界面的其余部分。 For this reason, you should not overuse any function that creates a dialog box (or modal window). 因此,您不应过度使用任何创建对话框(或模式窗口)的功能。

If you're triggering an alert() on click, the UI will be disabled and you can not perform an immediate follow up click without closing the alert() . 如果您在点击时触发alert() ,则用户界面将被禁用,并且如果不关闭alert()则无法立即执行后续点击。 ( You should be hearing a warning sound ) 您应该听到警告声

don't use inline javascript when you want to specify element, in your something.js write: 当您要指定元素时,不要在您的something.js中使用内联javascript:

$('.center-block').dbclick(function(){
  $('img').hide();
});

This works for what you are trying to accomplish in your code: 这适用于您试图在代码中完成的工作:

jsfiddle example: http://jsfiddle.net/larryjoelane/gz4gst49/14/ jsfiddle示例: http : //jsfiddle.net/larryjoelane/gz4gst49/14/

With the code below placed in a script tag at the bottom of your body document you still use the alert if you want: 将下面的代码放在正文文档底部的脚本标签中,如果需要,您仍然可以使用警报:

<script>


//the class to click on
var selector = ".center-block";

//set the doubleClicked variable to 0 meaning false
//in this example
var doubleClicked = 0;

//on single click function for selector class
$(selector).on("click",function(){//begin function


//use time out function to delay execution for specified amount
//of time in this case I used 2 seconds
setTimeout(function () {

    //if doubleClicked equals 0
    if (doubleClicked === 0){//begin if then

            //display alert
            alert("hello");

    }//end if then else

    }, 2000);//<--delay set for two seconds

});//end function

//on double click function
$(selector).on("dblclick",function(){//begin function

    //set doubleClicked to 1 
    doubleClicked = 1;

    //remove the img element
    $("img").remove();


});//end function

 </script>

You might want to detect the dbl-click yourself (using a timer). 您可能希望自己检测dbl-click(使用计时器)。

The basic concept in pure javascript works like this: 纯JavaScript的基本概念如下所示:

 <img class=" center-block" src="http://i.stack.imgur.com/pucwG.png" onclick="clearTimeout(this.timer); //catch the second click of dbl-click this.timer=setTimeout(function(){ alert('Hello'); }, 250);" ondblclick="clearTimeout(this.timer); this.parentNode.removeChild(this);" > 

In this example we simply hook the timer-id (the return-value of setTimeout ) to the originating element. 在此示例中,我们仅将timer-id( setTimeout的返回值)连接到原始元素。
Passing an invalid ID to clearTimeout does not have any effect (and doesn't throw an exception), so we don't even need to check for it's existence. 将无效的ID传递给clearTimeout不会有任何效果(并且不会引发异常),因此我们甚至不需要检查它的存在。
A delay-time of 250 ms is usually enough (150ms is usually to short). 250 ms的延迟时间通常就足够了(150ms通常较短)。

Note: is recommended to use more advanced methods to hook your events instead of in-lining them as you have done. 注意:建议您使用更高级的方法来挂接事件,而不要像完成操作那样内联它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM