如何使用按钮注册EventHandler [英] How to Register EventHandler with button
本文介绍了如何使用按钮注册EventHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了以下Javascript代码。
< body>
< script>
function hi(){
alert( Hello World !!);
}
var no1 = document .createElement(' input');
no1.setAttribute(' type', ' button');
no1.setAttribute(' name', ' one');
no1.setAttribute(' value', ' 1');
document .body.appendChild(no1);
no1.onClick = hi;
< / script>
< / body >
我用 hi()
函数注册了我的活动,但是它没有用。
我的代码中是否有任何错误?
我希望每当我点击我的按钮时,它就会给我看警报框。
谢谢!
解决方案
你从未试图创建一个负责的属性对于事件,在这种情况下应该onclick
属性: http:/ /www.w3schools.com/jsref/event_onclick.asp [ ^ ]。
但您不必使用setAttribute
显式添加属性。它可能只是
no1.onclick = function (){
// 一些JavaScript代码......
};
祝你好运,
-SA
问题是这样的生成的代码如下所示:
< input submitname = 一个 type = 按钮 onclick = function hi(){alert( hello = world = value = 1 / >
我想,这不是你想要的:)
你知道为什么吗?由于大写字母:no1.on C lick = hi;
只需将其设为小写即可使用(no1.onclick = hi;
)。
您可以创建一个asp.net对象按钮或图像按钮,并添加属性onClientClick
所以你按下按钮,按F4,在属性onClient上点击hi(); return false;没有双引号。
返回false取消按钮回发事件。你可以返回hi();
如果hi返回true,则回发事件触发,如果hi()返回false,则回发被取消。 />
请记住先注册你的java脚本文件。
要修复上面的代码,首先需要正确创建元素才能使其正常工作。不要使用setAttribute,JavaScript拥有构建元素所需的一切。
var inputObj = document.createElement(input);
inputObj.type =button
< head>
< script type =text / javascript>
函数AddButton(){
var button = document.createElement(input);
button.type =button;
button.value =创建一个新按钮;
button.onclick = AddButton;
var container = document.getElementById(buttonContainer);
container.appendChild(button);
}
< / script>
< / head>
< body onload =AddButton()>
< div id =buttonContainer>< / div>
< / body>
I have written following Javascript code.
<body>
<script>
function hi(){
alert("Hello World !!");
}
var no1 = document.createElement('input');
no1.setAttribute('type', 'button');
no1.setAttribute('name', 'one');
no1.setAttribute('value', '1');
document.body.appendChild(no1);
no1.onClick = hi;
</script>
</body>
I have registered my event with the function hi()
, but it is not working.
Is there any error in my code?
I would like that whenever I click on my button, it show me the alert box.
Thanks!
解决方案
You never tried to create a single attribute responsible for events, which in this case should beonclick
attribute: http://www.w3schools.com/jsref/event_onclick.asp[^].
But you don''t have to add the attribute explicitly withsetAttribute
. It could be just
no1.onclick=function(){ // some JavaScript code... };
Good luck,
—SA
The problem is, tat this way the generated code looks like this:
<input submitname="one" type="button" onclick="function hi(){ alert(" hello="" world="" value="1" />
I suppose, this is not what you want :)
You know why? Because of the capital letter:no1.onClick = hi;
Just make it lowercase and will work (no1.onclick = hi;
).
You can make a asp.net object button or image button, and add the attribute onClientClick
So you make the button, press F4, in the property onClientClick enter "hi(); return false;" without the double quotes.
return false cancels the button postback event. You can do return hi();
and if hi returns true, the postback event fires, and if hi() returns false, the postback is canceled.
Just remember to register your java script file first.
[EDIT]
To fix your code above, you need to create the element correctly first for it to work. Don''t use setAttribute, JavaScript has everything you need to build the element.
var inputObj = document.createElement ("input");
inputObj.type = "button"
<head> <script type="text/javascript"> function AddButton () { var button = document.createElement ("input"); button.type = "button"; button.value = "Create a new button"; button.onclick = AddButton; var container = document.getElementById ("buttonContainer"); container.appendChild (button); } </script> </head> <body onload="AddButton ()"> <div id="buttonContainer"></div> </body>
这篇关于如何使用按钮注册EventHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文