简体   繁体   English

JavaScript ondblclick无法正常工作

[英]JavaScript ondblclick not working

I'm trying to have a text entry box (and only the one single text input) be cleared when double clicked. 我试图双击时清除一个文本输入框( 输入一个文本)。

Here's the HTML 这是HTML

</div>
<div class="form-group">
<label for="green" class="control-label green col-md-2">Green:</label>
<div class="col-md-2">
<input type="text" id="green" ondblclick="Clear();" class="form-control"/>
</div>

And the JavaScript 还有JavaScript

    function Clear()
    {    
    this.value="";
    }

It doesn't seem to be working, does anyone know what is wrong? 似乎没有用,有人知道错在哪里吗?

this wont work in this case as it is refering to window object instead use docuement.getElementById . this在这种情况下不会工作,因为它是指的窗口对象,而不是使用docuement.getElementById

 function Clear() { document.getElementById('green').value=""; console.log(this); // this..value=""; } 
 </div> <div class="form-group"> <label for="green" class="control-label green col-md-2">Green:</label> <div class="col-md-2"> <input type="text" id="green" ondblclick="Clear();" class="form-control"/> </div> 

or you can pass element to Clear function like following: 或者您可以将元素传递给Clear函数,如下所示:

 function Clear(element) { element.value = ""; } 
 </div> <div class="form-group"> <label for="green" class="control-label green col-md-2">Green:</label> <div class="col-md-2"> <input type="text" id="green" ondblclick="Clear(this);" class="form-control"/> </div> 

You can use the below code. 您可以使用以下代码。

HTML HTML

<div class="form-group">
<label for="green" class="control-label green col-md-2">Green:</label>
<div class="col-md-2">
  <input type="text" id="green" ondblclick="Clear(this);" class="form-control" />
</div>
</div>

JS JS

function Clear(x) {
  x.value = "";
}

Hope this helps you. 希望这对您有所帮助。

try this: 尝试这个:

function Clear() {    
    var _green = document.getElementById(this.event.target.id);
    _green.value="";
}

In the above code, we have detect the ID of the called function with this.event.target.id . 在上面的代码中,我们使用this.event.target.id检测了被调用函数的ID。 After getting the ID, grab the DOM element with document.getElementById() and set the value blank. 获取ID后,使用document.getElementById()获取DOM元素并将值设置为空。

You need to pass this to the function as a parameter: 您需要this作为参数传递给函数:

  function Clear(that) { that.value = ""; } 
 <div class="form-group"> <label for="green" class="control-label green col-md-2">Green:</label> <div class="col-md-2"> <input type="text" id="green" ondblclick="Clear(this);" class="form-control" /> </div> </div> 

In order to make this work without changing anything inside your function you could bind the element like so: 为了在不更改函数内部任何内容的情况下进行这项工作,您可以像这样绑定元素:

... ondblclick="Clear.call(this);" ...

Inside Clear() you will reference the element using this . Clear()内部,您将使用this引用元素。

Bonus 奖金

You could also pass in the event like so: 您也可以像这样通过事件:

... ondblclick="Clear.call(this, event);" ...

Then, your function would look like this: 然后,您的函数将如下所示:

function Clear(event)
{
   // ...
}

Alternatively 另外

You could also register the event handler separately: 您还可以单独注册事件处理程序:

document.getElementById('green').addEventListener('dblclick', Clear);

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

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