JavaScriptÖÐsetAttributeÓ÷¨
ÎÒÃǾ³£ÐèÒªÔÚJavaScriptÖиøElement¶¯Ì¬Ìí¼Ó¸÷ÖÖÊôÐÔ£¬Õâ¿ÉÒÔͨ¹ýʹÓÃsetAttribute()À´ÊµÏÖ£¬Õâ¾ÍÉæ¼°µ½ÁËä¯ÀÀÆ÷µÄ¼æÈÝÐÔÎÊÌâ¡£
setAttribute(string name, string value)£ºÔö¼ÓÒ»¸öÖ¸¶¨Ãû³ÆºÍÖµµÄÐÂÊôÐÔ£¬»òÕß°ÑÒ»¸öÏÖÓеÄÊôÐÔÉ趨Ϊָ¶¨µÄÖµ¡£
1¡¢ÑùʽÎÊÌâ
setAttribute(class, value)ÖÐclassÊÇÖ¸¸Ä±äclassÕâ¸öÊôÐÔ£¬ËùÒÔÒª´øÒýºÅ¡£
vName´ú±í¶ÔÑùʽ¸³Öµ¡£
ÀýÈç:
var input = document.createElement(input);
input.setAttribute(type, text);
input.setAttribute(name, q);
input.setAttribute(class,bordercss);
Êä³öʱ£º,¼´£¬input¿Ø¼þ¾ßÓÐbordercssÑùʽÊôÐÔ
×¢Ò⣺classÊôÐÔÔÚW3C DOMÖаçÑÝןÜÖØÒªµÄ½ÇÉ«£¬µ«ÓÉÓÚä¯ÀÀÆ÷²îÒìÐÔÈÔÈ»´æÔÚ¡£
ʹÓÃsetAttribute(class, vName)Óï¾ä¶¯Ì¬ÉèÖÃElementµÄclassÊôÐÔÔÚfirefoxÖÐÊÇÐеÄͨµÄ£¬µ«ÔÚIEÖÐÈ´²»ÐС£ÒòΪʹÓÃIEÄں˵Ää¯ÀÀÆ÷²»ÈÏʶclass£¬Òª¸ÄÓÃclassName£»
ͬÑù£¬firefox Ò²²»ÈÏʶclassName¡£ËùÒÔ³£Óõķ½·¨ÊǶþÕ߼汸£º
element.setAttribute(class, value); //for firefox
element.setAttribute(className, value); //for IE
2¡¢·½·¨ÊôÐÔµÈÎÊÌâ
ÀýÈ磺
var bar = document.getElementById(testbt);
bar.setAttribute(onclick, javascript:alert('This is a test!'););
ÕâÀïÀûÓÃsetAttributeÖ¸¶¨eµÄonclickÊôÐÔ£¬¼òµ¥£¬ºÜºÃÀí½â¡£
µ«ÊÇIE²»Ö§³Ö£¬IE²¢²»ÊDz»Ö§³ÖsetAttributeÕâ¸öº¯Êý£¬¶øÊDz»Ö§³ÖÓÃsetAttributeÉèÖÃijЩÊôÐÔ£¬ÀýÈç¶ÔÏóÊôÐÔ¡¢¼¯ºÏÊôÐÔ¡¢Ê¼þÊôÐÔ£¬Ò²¾ÍÊÇ˵ÓÃsetAttributeÉèÖÃstyleºÍonclickÕâЩÊôÐÔÔÚIEÖÐÊÇÐв»Í¨µÄ¡£
Ϊ´ïµ½¼æÈݸ÷ÖÖä¯ÀÀÆ÷µÄЧ¹û£¬¿ÉÒÔÓõã·ûºÅ·¨À´ÉèÖÃElementµÄ¶ÔÏóÊôÐÔ¡¢¼¯ºÏÊôÐÔºÍʼþÊôÐÔ¡£
document.getElementById(testbt).className = bordercss;
document.getElementById(testbt).style.cssText = color: #00f;;
document.getElementById(testbt).style.color = #00f;
document.getElementById(testbt).onclick= function () { alert(This is a test!); }
ÓÉ´ËÑÓÉìµÄÎÊÌâ:
Ò»¸öinputµÄtext,µ±½«html¸³ÖµÎªÄ³¸ödivµÄinnerHTMLʱ,Óöµ½Ò»¸öÏÖÏó,µ±ÔÚfirefoxÏÂʱ(IEϲ»´æÔÚ´ËÎÊÌâ), ¸³ÖµºóµÄinnerHTMLÀï²»º¬ÓÐvalue,¼´µ±ÄãÔÚÎı¾¿òÊäÈëÄÚÈݺó,ÄãÏ뽫¸³Öµ¸ødivʱ,Ö»»áµÃµ½,ÕâÀï×ÜÊǻὫvalueÇå³ý.
Õâʱ,setAttributeÆð×÷ÓÃÁË,ÔÚinputÄÚ¼ÓÉÏ:onkeyup=t
Ïà¹ØÎĵµ£º
function EnhancedImage(src,onLoaded){
var self = this;
this.src = src;
this.width = 0;
this.height = 0;
this.onLoaded = onLoaded;
this.loaded = false;
this.image = null;
this.load = function(){
if(this.loaded)
return;
this. ......
µÚÎåÕ Êý×é JavaScriptµÄÊý×éÒ²ÊÇÒ»¸ö±È½ÏÓÐÒâ˼µÄÖ÷Ì⣬ËäÈ»ÃûΪÊý×é(Array)£¬µ«ÊǸù¾ÝÊý×é¶ÔÏóÉϵķ½·¨À´¿´£¬¸üÏñÊǽ«ºÜ¶à¶«Î÷»ìÔÚÔÚÒ»ÆðµÄ½á¹û¡£¶ø´«Í³µÄ³ÌÐòÉè¼ÆÓïÑÔÈçC/JavaÖУ¬Êý×éÄÚµÄÔªËØÐèÒª¾ßÓÐÏàͬµÄÊý¾ÝÀàÐÍ£¬¶ø×÷ΪÈõÀàÐ͵ÄJavaScript£¬ÔòûÓÐÕâ¸öÏÞÖÆ£¬ÊÂʵÉÏ£¬JavaScriptµÄͬһ¸öÊý×éÖУ¬¿ÉÒÔÓи÷ÖÖÍêÈ«²»Í ......
JavaScript ʱÖÓ£¨´úÂ룩:
<html>
<body onload=aaa()>
<script type="text/javascript">
var nowtime ;
var year ;
var month ,date,hours,minutes,seconds,all;
function aaa()
{
nowtime = new Date();
year = nowtime.getYear();
month = nowtime.getMonth()+1;
date = nowtim ......
JavaScript¾ÍÕâô»ØÊÂ1£º»ù´¡ÖªÊ¶
1 ´´½¨½Å±¾¿é
1: <script language=”JavaScript”>
2: JavaScript code goes here
3: </script>
2 Òþ²Ø½Å±¾´úÂë
1: <script language=”JavaScript”>
2: <!--
3: document.write(“Hello”);
4: // -->
5: </scri ......
¼òÂÔÓï¾ä
JavaScript ¿ÉÒÔʹÓüòÂÔÓï¾ä¿ìËÙ´´½¨¶ÔÏóºÍÊý×飬±ÈÈçÏÂÃæµÄ´úÂ룺
¿ÉÒÔʹÓüòÂÔÓï¾äÈçÏ£º
¶ÔÏó car ¾Í´Ë´´½¨£¬²»¹ýÐèÒªÌرð×¢Ò⣬½áÊø»¨À¨ºÅÇ°Ò»¶¨²»Òª¼Ó ";" ·ñÔòÔÚ IE »áÓöµ½ºÜ´óÂé·³¡£
´´½¨Êý×éµÄ´«Í³·½·¨ÊÇ£º
ʹÓüòÂÔÓï¾äÔò£º
ÁíÒ»¸ö¿ÉÒÔʹÓüòÂÔÓï¾äµÄµØ·½ÊÇÌõ¼þÅжÏÓï¾ä£º
¿ÉÒÔ¼òÂÔΪ£ ......