.Dyp标签上的.keypress? [英] .keypress on a DIV tag?

查看:187
本文介绍了.Dyp标签上的.keypress?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在像这样的div元素上工作.keypress?:

Is there a way to get work .keypress on a div element like this?:

<html>
<body>

<script type="text/javascript">
<!--

$('#idtext').keypress(function(event) {
  var keyCode = event.keyCode;
  $('#idtext').text(function(i, text) {

   return text + String.fromCharCode(keyCode); 

  });

});

// -->
</script>

<div id="idtext"></div>

</body>
</html>


推荐答案

是:您需要添加 tabindex 属性到< div> 以允许它获得焦点。

Yes: you need to add a tabindex attribute to the <div> to allow it to receive the focus.

<div id="idtext" tabindex="1"></div>

此外,您在keypress事件中输入的文本字符代码所需的属性是哪个,而不是 keyCode

Also, the property you want for the character code of the text entered in a keypress event is which, not keyCode.

最后,HTML评论标签所有现代浏览器中都不需要< script> 元素。

Finally, the HTML comment tags inside the <script> element are unnecessary in all modern browsers.

这篇关于.Dyp标签上的.keypress?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆