简体   繁体   English

点击鼠标即可调用功能

[英]Call function with mouse click

I have a text area. 我有一个文本区域。 Each time the enter key is entered the cursor travels to the next line of the text area and a function is called. 每次输入回车键时,光标移动到文本区域的下一行并调用一个功能。 This function posts/updates the entry in a database. 此功能发布/更新数据库中的条目。 I want it so that if I edit a line and then click on the mouse to resume typing at another line the function is again called on the mouse click 我想要它,以便如果我编辑一行,然后单击鼠标继续在另一行键入该功能再次调用鼠标单击

  $("#textarea").keydown(function (e) {

 if (e.keyCode == 13) {
  document.addEventListener('keydown', newLine(this, "\n"));
  console.log("code added");
    e.preventDefault();
     stream();

Is it possible to change my line to something like this and the method gets called on pressing the enter key or pressing the mouse(anywhere in the text area)? 是否可以将我的行更改为类似的东西,按Enter键或按鼠标(文本区域中的任何位置)调用该方法?

  if (e.keyCode == 13 || mouse.click) {

I know the above isn't correct but want to illustrate what I'm after 我知道上面的内容并不正确,但我想说明我的目标

You could take use of jQuery's .on method like so: 您可以使用jQuery的.on方法,如下所示:

$("#textarea").on('click keydown', (e) => {
    if(e.keyCode && e.keyCode == 13 || e.type == "click" ){
        // Do stuff
    }
});

It takes a first parameter as string with different events, which mean you can listen to multiple events at once. 它将第一个参数作为具有不同事件的字符串,这意味着您可以一次监听多个事件。 The second is a callback function, where you can track the event that is triggered. 第二个是回调函数,您可以在其中跟踪触发的事件。 Nb: Events are different between click and keydown. Nb:点击和keydown之间的事件不同。 You can have a closer look by putting console.log(e); 你可以通过放置console.log(e);仔细看看console.log(e); in your callback 在你的回调中

You'll need to attach another event listener. 您需要附加另一个事件监听器。 The keydown event will not trigger when a mouse is clicked. 单击鼠标时不会触发keydown事件。 You will need to add a $(...).click(function ...) as well. 您还需要添加$(...).click(function ...) For example... 例如...

function myFunction (e) {
  document.addEventListener('keydown', newLine(this, "\n"));
  console.log("code added");
  stream();
}

$("#textarea").keydown(function() {
  if (e.keyCode == 13) {
    myFunction()
    e.preventDefault();
  }
});

$('#textarea').click(myFunction)

Instead of putting a condition you can create 2 events and a common function to handle it. 您可以创建2个事件和常用函数来处理它,而不是放置条件。 Foe Example: 敌人示例:

    $("#textarea").keydown(function (e) {
 if (e.keyCode == 13) {
  logic1()



$("#textarea").click(function() { logic1();});

    function logic1(){
    document.addEventListener('keydown', newLine(this, "\n"));
  console.log("code added");
    e.preventDefault();
     stream();
}

I don't know about jQuery but with vanilla JS you can do something like this: 我不知道jQuery,但使用vanilla JS,你可以这样做:

 const textarea = document.querySelector('textarea'); const foo = event => { const output = document.querySelector('output'); output.textContent = event.type; } textarea.addEventListener('click', foo, false); textarea.addEventListener('keypress', foo, false); 
 <textarea></textarea> <output></output> 

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

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