首页 > 解决方案 > 编写 onkeypress 属性而不是 jquery 代码

问题描述

一切正常,但我需要这样编写代码:

<input type='text' class='inpTitle' data-fn = 'write' onkeypress = "???-what here">

有什么帮助吗?

$('.inpTitle').on('keypress', function(e){
if(e.keyCode == 13){
let fn = $(this).attr('data-fn');    
window[fn]();
}

});

function write(){
console.log('323');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type='text' class='inpTitle' data-fn = 'write'>

标签: javascriptjqueryhtml

解决方案


这将

<input type="text" data-fn="s" onkeypress="m(this.getAttribute('data-fn'),e)">






  function m(obj,event) {
if(e.keyCode == 13){
    var a=obj.getAttribute('data-fn')
          alert(a);
}
        }

推荐阅读