如何为输入标签创建 onclick 事件 [英] How to create onclick event for input tag
问题描述
我创建了一个输入字段,我希望在点击输入字段时触发一些事件.
I created an input field and I want some event to be fired on click of the input field.
为此,我使用了 onClick
方法,但我的调试器不会使用 TableExpand()
函数.即使在输入标签中,我也看不到 onClick
事件.
For that, I am using onClick
method but my debugger is not going to TableExpand()
function. Even in input tag also I am not able to see onClick
event.
这是我的代码.
MyData = function(args) {
debugger;
var dataUrl = args.url;
var divID = args.divID;
var div = document.getElementById(divID);
var input = document.createElement("input");
input.className = 'styled-select';
input.style = 'width:30%' ;
input.id = "SearchInput";
input.type = "text";
input.title = "Madd";
input.onclick = "TableExpand()"; // This is not adding in input tag.
div.appendChild(input);
//input.onkeyup = "TableExpand()";
//input.addEventListener('click', 'TableExpand()');
//div.onclick
TableExpand = function(){
debugger;
}
}
谁能帮帮我.
推荐答案
分配给 onclick
属性的值需要是一个函数.
The value assigned to the onclick
property needs to be a function.
您正在分配一个字符串.
input.onclick = TableExpand;
…您还需要在尝试使用它之前定义 TableExpand
.
… you also need to define TableExpand
before you try to use it.
使用函数声明而不是函数表达式就足够了(因为声明会被提升).
Using a function declaration instead of a function expression will suffice for that (as declarations are hoisted).
function TableExpand (){
debugger;
}
这篇关于如何为输入标签创建 onclick 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!