文档详情

JavaScript中getElementById方法的使用.doc

发布:2018-03-14约3.84千字共7页下载文档
文本预览下载声明
JavaScript中getElementById方法的使用   摘要:介绍了getElementById方法的作用以及使用技巧,并结合具体的案例提出了该方法在Internet Explorer和Firefox浏览器下效果的不同,指出IE对ID和Name的不敏感性,最后得出对于IE该方法是有漏洞的结论。   关键词:JavaScript;getElementById;IE;Firefox;漏洞      The Use of getElementById Method in JavaScript   SHENG Yun-yao   (Computer Sicence and Engineering Department, Changzhou Institute of Mechatronic Technology, Changzhou 213164, China)   Abstract: Described the role and tips of getElementById method, and used example to bring up the different effectiveness of this method in Internet Explorer and Firefox browser, and pointed out that the IE on the ID and Name of the insensitivity,at last came to the conclusion that the method is flawed for IE.   Key words: JavaScript;getElementById;IE;Firefox;Bug   Javascript是为适应动态网页制作的需要而诞生的一种编程语言,如今越来越广泛地使用于Internet网页制作上。它是由Netscape公司开发的一种脚本语言(scripting language),或者称为描述语言。在HTML基础上,使用它可以开发交互式Web网页。它的出现使得网页和用户之间实现了一种实时性的、动态的、交互性的关系,使网页包含更多活跃的元素和更加精彩的内容。   Javascript主要是基于客户端运行的,用户点击带有Javascript的网页,网页里的Javascript就传到浏览器,由浏览器对此作处理。如下拉菜单、验证表单有效性等大量互动性功能,都是在客户端完成的,不需要和Web Server发生任何数据交换, 因此,不会增加Web Server的负担。目前,几乎所有浏览器都支持Javascript,如Internet Explorer(IE),Firefox,Netscape,Mozilla,Opera等。但是由于浏览器间没有统一的标准,所以在执行JavaScript时会出现截然不同的效果。本文研究了JavaScript中getElementById方法,发现不同的浏览器对该方法的支持度是不同的。   1 getElementById的使用   1.1 方法意义   getElementById(参数):参数为标签的Id。   该方法的作用是返回具有指定 ID 属性值的第一个对象的一个引用。通过ID来设置/返回HTML标签的属性及调用其事件与方法。用这个方法基本上可以控制页面所有标签,条件很简单就是给每个标签分配一个ID号。   1.2 方法使用技巧   在我们写JavaScript的时候经肯定会经常用到document.getElementById()这个方法,这么长一串很容易写错,而且其中getElementById又有大小写之分,其实可以使用$()简写方法。下面就通过电子邮件验证的案例来介绍该方法的使用:          getElementById的技巧          function $()   {   var elements = new Array();   for (var i = 0; i arguments.length; i++)   {   var element = arguments[i];   if (typeof element == string)   element = document.getElementById(element);   if (arguments.length == 1)   return element;   elements.push(element);   }   return elements;   }      function chemail(){   if($(
显示全部
相似文档