javascript当中绑定事件和方法
8.绑定事件和方法
马克-to-win:once, long time to know that "script" must be put in behind, while "input" must be put in front,
otherwise document.getElementById("button1"); can not find the "button1",alert("button1 is "+ button1); 结果就是null,为什么这次跟往常我们的印象不一样了呢?因为往常我们先写一段script,之后在body中写上诸如<INPUT TYPE="button" οnclick="abc",之类的话,这样上面的abc这样的代码开始不会被执行,里面的诸如document.getElementById ("button1");也就正确了。这里为什么跟往常不一样呢?因为要在一开始时,先给button绑上事件代码,否则button无事件响应。
例 8.1(commonEventPrerequisiteIEFF.html)
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<INPUT TYPE="button" NAME="button1" ID="button1" VALUE="单击我"/>
<script>
function testie()
{
alert("单击按钮ie");
}
function testFF()
{
alert("单击按钮FireFox");
}
/**/
/*obj.addEventListener("click",function(){func("看到我了吧");},false);
the third argument is: A Boolean flag value that indicates whether the event listener should
use bubbling (由里向外) or capture (由外向里) event propagation. 只要我们
知道第三个参数和事件冒泡有关就可以了。缺省值为假,即冒泡的意思。具体例子参考后面的事件冒泡例子。
*/
var button1 = window.document.getElementById("button1");
alert("button1 is "+ button1);
alert("document.all is" + document.all);
alert(typeof(window.addEventListener) + " is typeof window.addEventListener");
alert(typeof(window.attachEvent) + " is typeof window.attachEvent");
alert(window.addEventListener + " is window.addEventListener");
alert(window.attachEvent + " is window.attachEvent");
if (typeof window.attachEvent === "object")
{
alert("ie");
button1.attachEvent("onclick", testie);
}
if (typeof window.addEventListener === "function")
{
alert("firefox");
button1.addEventListener("click", testFF, false);
}
// button1.addEventListener("click",test,false);
//button1.attachEvent("onclick" , test);
var str = "";
</script>
更多内容请见原文,文章转载自:https://blog.csdn.net/qq_43650923/article/details/102210095
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义