JavaScript 的学习记录。

来源:互联网 发布:会计软件哪种好 编辑:程序博客网 时间:2024/06/02 23:57
1.
var arr = {
    text:["栏目一","栏目二","栏目三","栏目四","栏目五"],
     CSS:["Colo1","Colo2","Colo3","Colo4","Colo5"],
    func:["test",myFunc,"func"]
    }

function myFunc(str){
    return "myFunc("+str+")="+typeof(str);
}

alert(arr.text[3]);
alert(arr.func[1]("中国"))

   
2.
     function myFunc(){
        this.test = function(str){
            alert(str);
        }
     }
     var print = new myFunc;
         print.test("Hello work.");


3.
     function getPre(url){
        var temp = url.split("?");
            alert(temp[1]);
     }


4.全局变量
  //方式一
     function set(s){
        temp = s;
     }
     function get(){
        alert(temp);
     }
     set(3);
     get();
     set("hello");
     get();

  //方式二
     var gTemp = 3;
     function gSet(s){
         gTemp = s;
     }
     function gGet(){
         alert(gTemp);
     }
     gSet(20);
     gGet();
     gSet("hello");
     gGet();


   
5.
    var t = new Array();
        t["a"] = "A";
        t["b"] = "B";
        t["c"] = "C";
        t["d"] = "D";
        delete t["d"];
    for(var i in t){
        alert('key:'+i+',value:'+t[i]);
    }



6.解除事件/添加事件
<script>
function out(){ 
     alert(3);
}
function ElmById(){ 
      return document.getElementById("iEvent");
}
function add(event){ 
    event.srcElement?ElmById().attachEvent('onclick', out):ElmById().onclick=out;
}
function del(event){ 
    event.srcElement?ElmById().detachEvent('onclick', out):ElmById().onclick=null;
}
</script>
<input id="iEvent" name="iEvent" type="submit" value="Click Me" />
<input type="button" value="add" onClick="add(event)" />
<input type="button" value="del" onClick="del(event)" />



7.遍历一个对象的所有属性
var obj = document;
for(att in obj){
   document.write(obj[att]+":"+att+"<br />");
}


8.
FF/IE:parentNode //获取文档层次中的父对象
IE:parentElement //获取对象层次中的父对象
FF:target 
IE:srcElement



9.
NS下event对象必须通过参数传递,而不能直接引用.IE下可以不参数event能直接在函数中使用。

10.
<script>
<!--
var iShop = new Object();
iShop.MethodA = function(){ alert("this's a.")}();
iShop.MethodB = new function(){ alert("this's b.")};
iShop.MethodC = function(s){ alert(s); };

iShop.MethodA;
iShop.MethodB;
iShop.MethodC("Hello work.");


(function(){
     iFunction = {
        MethodA:a,
        MethodB:b
     };
     function a(){
        alert("This's iFunction.MethodA.");
     };
     function b(){
        alert("This's iFunctionb.MethodB.");
     };
 }
)();
iFunction.MethodA();
iFunction.MethodB();
//-->
</script>

原创粉丝点击