admin管理员组文章数量:1189174
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>
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>
Share
Improve this question
edited Sep 14, 2019 at 10:16
Brian Tompsett - 汤莱恩
5,88372 gold badges61 silver badges133 bronze badges
asked Sep 21, 2010 at 10:20
xRobotxRobot
26.6k72 gold badges191 silver badges316 bronze badges
1 Answer
Reset to default 29Yes: you need to add a tabindex
attribute to the <div>
to allow it to receive the focus.
<div id="idtext" tabindex="1"></div>
Also, the property you want for the character code of the text entered in a keypress event is which
, not keyCode
.
Finally, the HTML comment tags inside the <script>
element are unnecessary in all modern browsers.
本文标签: javascriptkeypress on a DIV tagStack Overflow
版权声明:本文标题:javascript - .keypress on a DIV tag? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738386317a2084201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论