javascript - 编写 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'>
解决方案
这将
<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);
}
}
推荐阅读
- c# - 如何从需要身份验证的 URL 中检索文件?
- spring - Spring Rest API Byte[] 作为返回对象问题的属性
- python-3.x - 如何将动态变量传递给要检查的操作系统路径
- google-chrome - 在高强度负载下闲置时间的含义| chrome 开发者工具
- google-cloud-platform - 在 Google Cloud 上通过 JSON 查找提供 Javascript 的最快方法?
- laravel - 如何避免覆盖 Laravel 中的路由?
- r - 循环遍历数据框输出图列表
- pandas - 熊猫导出和导入不同的字典
- python-3.x - 无法为绘图颜色配置 matplotlib rcparams
- mongodb - Mongoose 聚合子文档的属性并显示结果