输入为焦点时,Onmousedown不起作用

时间:2019-07-03 08:16:13

标签: javascript

我对以下代码有一点疑问

问题是:当我的鼠标专注于输入(单击输入,例如输入一些文字)时,不会调用isKeyPressed函数。正常吗我该如何解决?

function isKeyPressed(event) {
  if (event.ctrlKey) {
    console.log('CTRL + CLICK PRESSED');
  } else {
    console.log('NOT PRESSED');
  }
}
<div class="col-sm-6 img" onmousedown="isKeyPressed(event)">
  <div id="rectangle" class="rectangle-not-active"></div>
  <img class="file" id="my-image" alt="File" src="https://www.thalesgroup.com/sites/default/files/styles/article_card/public/database/d7/e-services_modified_1.jpg?itok=DkaYx8h8">
</div>

<input name="supplier" onfocusout="console.log('out')" onfocusin="console.log('in')" type="text" class="form-control" id="supplier" />

1 个答案:

答案 0 :(得分:3)

只需删除触发错误的冗余函数调用即可。

并将isKeyPressed函数赋予img标签onmousedown事件

function isKeyPressed(event) {
  if (event.ctrlKey) {
    zoom = true;
    console.log("we are in true bruh");
  } else {
    zoom = false;
    console.log("we are in false bruh");
  }
}
<div class="col-sm-6 img">
  <div id="rectangle" class="rectangle-not-active"></div>
  <img class="file" onmousedown="isKeyPressed(event)" id="my-image" alt="File" src="{{ url_for('static', filename='files/full/' + pdf['fullJpgFilename']) }}">
</div>

<input name="supplier"  type="text" class="form-control" id="supplier" />