写onkeypress属性而不是jquery代码

时间:2019-01-12 11:03:25

标签: javascript jquery html

一切都在这里工作,但我需要这样编写代码:

<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'>

2 个答案:

答案 0 :(得分:1)

这将

<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);
}
        }

答案 1 :(得分:1)

我认为这应该可行:

<input type='text' class='inpTitle' data-fn = 'write' onkeypress = "prewrite(this, event)">

function prewrite(obj, e) {
  if(e.keyCode == 13){
  let fn = obj.attr('data-fn');    
  window[fn]();
}

function write(){
  console.log('323');
}