简体   繁体   English

为什么此javascript onkeypress事件不起作用

[英]Why this javascript onkeypress event doesn't work

This example should fire alert when I add text in the text area, for some reason it does so when I use $('#sentid1').onkeypress , and it only works when I use $('#sentid1').onkeypress . 当我在文本区域中添加文本时,此示例将触发警报,由于某种原因,当我使用$('#sentid1').onkeypress ,它会$('#sentid1').onkeypress ,并且仅当我使用$('#sentid1').onkeypress才起作用。

html part: html部分:

<div id="textarea" contenteditable=""><span id="sentid1" class="sentence"> This is sentence 1. </span><span id="sentid2" class="sentence"> This is sentence 2.</span></div>

Javascript/jQuery part: Javascript / jQuery部分:

 if (1==1) {
  $('#sentid1').onkeypress = function(event) {
            alert("theid: " + this.id);
        };  
  } else{
     document.onkeypress = function(event) {
              alert("theid: " + this.id);
           };   
  }

How are you testing that keypress event? 您如何测试该按键事件? In order to trigger keypress event you need your element to be either focused or be a parent of some other element on which keypress is triggered, so keypress is bubbled . 为了触发keypress事件,您需要使您的元素成为焦点或成为触发了keypress的其他元素的父元素,以便使keypress 冒泡

You're trying to catch keypress on span , by default span is not focusable, let's make it with tabindex for example 您正在尝试在span上捕获按键,默认情况下span无法聚焦,例如让我们使用tabindex进行设置

<span id="sentid1" class="sentence" tabindex="0"> This is sentence 1. </span>

Now you can actually click on this span, press some key and see the expected result 现在,您实际上可以单击该跨度,按一些键并查看预期结果

You should attach the event handler like this 您应该像这样附加事件处理程序

$(...).keypress(function(event) {...} )

or 要么

$(...).on('keypress', function(event) {...} )

And it needs to be attached to the #textarea , since that is the element that can take an input, which the span cannot. 并且需要将其附加到#textarea ,因为那是可以接受输入的元素,而span则不能。

 $('#textarea').keypress(function(event) { alert("theid: " + this.id); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="textarea" contenteditable=""><span id="sentid1" class="sentence"> This is sentence 1. </span><span id="sentid2" class="sentence"> This is sentence 2.</span></div> 


Updated based on a comment 根据评论进行了更新

If to get the span , do like this, where you use the event.target.id to get which span 如果要获取span ,请执行以下操作,在其中使用event.target.id获取哪个span

 $('#textarea').keypress(function(event) { alert("theid: " + event.target.id); }); 
 #textarea { position: relative; } span[contenteditable] { outline: none; } span[contenteditable]:focus::before { content: ''; position: absolute; left: 0; top: 0; right: 0; bottom: 0; outline: 2px solid lightblue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="textarea"><span contenteditable="" id="sentid1" class="sentence"> This is sentence 1. </span><span contenteditable="" id="sentid2" class="sentence"> This is sentence 2.</span></div> 

Try this. 尝试这个。 For jquery selector $('#sentid1') use the jquery keypress 对于jquery选择器$('#sentid1')使用jquery按键

if (1==1) {
  $('#sentid1').keypress(function() {
            alert("theid: " + this.id);
        });  
  } else {
     document.keypress(function() {
              alert("theid: " + this.id);
        });   
  }
if (1==1) {
  $('#sentid1').Keypress(function(event) {
            alert("theid: " + this.id);
        });  
  } else{
     document.Keypress(function(event) {
              alert("theid: " + this.id);
           });   
  }

not clear about your approach if you want a alert writing something on textarea you can use key up 如果您想在文本区域上写一些内容的警报,则不清楚您的方法,可以使用键盘向上键

if (1==1) {
  $('#sentid1').keyup(function(event) {
            alert("theid: " + this.id);
        });  
  } else{
     document.keyup(function(event) {
              alert("theid: " + this.id);
           });   
  }

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

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