JS函数无法从KeyCode中使用/如何使用键盘键调用此JS函数

RR苏萨尔

这是一个getSelectedText()可以button单击的简单脚本功能这意味着当我们选择任何文本并单击按钮时,该函数将NEWCLASS成功创建新的div。但是现在我想使用快捷键,例如CTRL+W来获取NEWCLASSdiv中的选定文本

我试过了这段代码,但是没有用。请检查一下,让我知道我在这里犯了什么错误。

var input = document.getElementsByTagName("body")[0];
input.addEventListener("keyup", function(event) {
  if (event.keyCode === 37) {
   event.preventDefault();
   document.getElementById("myBtn").click();
  }
});

我的代码:

// Function to get the Selected Text  
function getSelectedText() {
  var selectedText = '';
  // #### create a new element with variable (nw) #### //
  var nw = document.createElement("div"); // Element's tag
  nw.className = "NEWCLASS"; // Element's class name
  // some applied style

  // window.getSelection 
  if (window.getSelection) {
    selectedText = window.getSelection();
  }
  // document.getSelection 
  else if (document.getSelection) {
    selectedText = document.getSelection();
  }
  // document.selection 
  else if (document.selection) {
    selectedText =
      document.selection.createRange().text;
  } else return;
  // #### get the Selected text appended to body #### //
  nw.innerHTML = selectedText;
  document.getElementsByClassName('maintitle')[0].prepend(nw); // Append element to body
}
<button id="mybtn" onclick="getSelectedText()">Button</button>

<p>Select any part of this sentence and press the button. Select any part of this sentence and press the button. Select any part of this sentence and press the button</p>

<div class="maintitle"> </div>

穆罕默德·鲁扎克

带w的ctrl键将关闭chrome浏览器,因此我使用了“ Z”键,因为您可以将键代码替换为所需的代码。

在这里找到键码

const keySelected = new Set();

document.addEventListener('keydown', (e) => {
    keySelected.add(e.which);
    if(keySelected.has(17) && keySelected.has(90)){
        getSelectedText();
    }
});

document.addEventListener('keyup', (e) => {
     keySelected.delete(e.which);
});


/*
//jquery code if anyone want 
$(document).ready(function(){

  const keySelected = new Set();

  $(document).keydown(function (e) {
       keySelected.add(e.which);
       if(keySelected.has(17) && keySelected.has(90)){
           getSelectedText()
       }
  });
  
  $(document).keyup(function (e) {
     keySelected.delete(e.which);
  });

});

*/



// Function to get the Selected Text  
function getSelectedText() {
  var selectedText = '';
  // #### create a new element with variable (nw) #### //
  var nw = document.createElement("div"); // Element's tag
  nw.className = "NEWCLASS"; // Element's class name
   // some applied style

  // window.getSelection 
  if (window.getSelection) {
    selectedText = window.getSelection();
  }
  // document.getSelection 
  else if (document.getSelection) {
    selectedText = document.getSelection();
  }
  // document.selection 
  else if (document.selection) {
    selectedText =
      document.selection.createRange().text;
  } else return;
  // #### get the Selected text appended to body #### //
  nw.innerHTML = selectedText;
  document.getElementsByClassName('maintitle')[0].prepend(nw); // Append element to body
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="mybtn" onclick="getSelectedText()">Button</button>

  <p>
          Select any part of this sentence 
          and press the button. Select any part of this sentence 
          and press the button. Select any part of this sentence 
          and press the button
   </p> 
          
  <div class="maintitle"> </div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在JS中使用动态名称调用函数

在js函数中使用调用读取

如何在调用函数的javascript对象中使用此参数?

如何在Python中使用排序的键函数调用?

如何在 VUE.js 方法中使用函数中的参数调用函数?

如何使用 JS 调用 swift 函数?

无法在课堂上使用此函数调用

如何在js中使用require函数

如何使用新的js(0.6.0)包调用JS函数

无法在函数调用中使用某些类型

如何在 Prestashop 模块中使用的 Smarty 模板中调用 JS 中的后端函数?

如何在随后的sinon.js存根调用中使用不同的函数存根

我如何期望在Node.js中使用sinon,mocha和chai的特定参数调用函数?

如何在Ruby和rails中使用相同的id循环调用JS函数

onsubmit 函数无法使用 Node.js 正确调用

如何使用bash别名或函数中的键盘快捷键?

当用户使用 event.keyCode ===13 释放键盘上的键时执行函数时收到错误消息

函数不是从使用 onClick 事件调用的主 JS 函数调用的

如何使用节点JS调用带参数的PostgreSQL函数?

如何使用typescrpt调用外部js文件中的函数

如何使用Express Router正确调用password.js函数?

我如何使用javascript / jquery在枚举中调用函数js

如何在JS / jQuery函数中使用返回的AJAX数据

如何在JS“setAttribute”中使用“onclick”运行函数?

我如何在React JS函数中使用jQuery $(this)

如何在验证器函数中使用JS提交表单

如何在React中使用Stripe JS触发函数?

如何在js函数中使用PHP foreach变量

如何在父JS之外的子函数中使用THIS