jQuery 소스 학습 노트

19932 단어 jquery

 
//   window       
//first
var oldMap = {};
for(var i in window)
{
    oldMap[i] = 1;
}

//second
for(var i in window)
{
    if(oldMap[i]) continue;
    alert(i);
}

$()            dom  ,       ,        

$()               jquery  


//google  cdn  jquery  
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script>
    google.load("jquery",'1.4.2');
    google.setOnLoadCallback(function(){alert('aa'+$)}); 
</script>

//      
$ = [1,2];
jQuery(function($){
    //    jQuery  ,     $         $
    alert($);
})

//

$("<input>", {
  type: "text",
  val: "Test",
  focusin: function() {
    $(this).addClass("active");
  },
  focusout: function() {
    $(this).removeClass("active");
  }
}).appendTo("form");

//  select    ,          ,             ,

//         $("select").val([1,2,3]);

$("select option:selected").each(function(){  
    alert(this.value);
})


//
var message = 'Spoon!';

$('#foo').bind('click', {msg:message}, function(evt) {

    //         
    alert(evt.data.msg);
    
});
message = 'sjk';

          false            .preventDefault()[ie:evt.returnValue=false;] .stopPropagation()[ie:evt.cancelBubble=true;]

jq                , js          jq   

$("#song").click(function(evt){
    alert(evt.originalEvent); //       
    alert('click');    
})

//           
function test(evt, msg){
    alert(evt);
    alert(msg);    
    alert(this.innerHTML);
}
document.getElementById('song').onclick = function(evt){
    test.call(this, evt, 'ok');    //important
};

//    trigger    
$("#song").click( function (event, a, b) {
  //           ,a b undefined  
  //           ,  a  "foo", b  "bar"
      alert(a+'
'+b); } ).trigger("click", ["foo", "bar"]); live $(document).bind("click", function (e) { $(e.target).closest("li").toggleClass("hilight"); }); // mouse mouseenter / mouseleave jquery , instanceof Array // this [].push.apply(this,ary); //jquery function list(ary) { this.ary = ary; [].push.apply(this,ary); } list.prototype = { attr:function(){ for(var i=0;i<this.ary.lenght;i++){ this.ary[i][name] = value; } }, get:function(index){ if(index === undefined) return this.ary; else{ return this.ary[index]; } }, push:[].push, shift:[].shift } var l = new list([1,2,3]); l.push(4); l.push(5); console.dir(l); script script xpath: css // this function closure(fn,scope) { return function(){ fn.apply(scope, arguments); } } // ps: ? function invoke(obj, methodName) { return function(){ obj[methodName].apply(obj,arguments); }; } // function test(){ arguments.callee.guid = 1; } var fn = function(){ arguments.callee.guid = 1; } //fn();# alert(fn.guid); // var fn = function(){ var anony = function(){ // your code } anony.guid = 1; return anony; } $.proxy(fn,scope) $(function(){ function test(){ alert(this.html()); } var h = $(":header"); h.click($.proxy(test,h)) // this alert(test.guid); // guid, guid , guid ( ) }) w3c : addEventListener removeEventListener IE: attachEvent detachEvent //closest , live 、delegate $(document).bind("click", function (e) { $(e.target).closest("li").toggleClass("hilight"); }); $ jQuery.noConflict(); jQuery(function($){ ,jquery ($), var len = $("ul li").length; console.log(len); }) jquery // $ jQuery.noConflict(); // jQuery jQuery.noConflict(true); jquery $ , $ // $("#song").data("title", 'title'); $("#song").data("age", 27); // js , dom var obj = $("#song").data(); alert(obj.title); alert(obj.age); // obj, $("#song").data({name:12,age:27}); : // function test(){} var _nspool = {}; test.ns = function(name, value){ var names = name.split('.'); o = window; for(var i=0;i<names.length;i++) { if(!o[names[i]]) { o[names[i]] = {}; } o = o[names[i]]; _nspool[name] = o[names[i]]; o[names[i]] = value; } } test.ns('a.b.c', 123); console.dir(_nspool); // _nspool key , , [object Object] [ ] alert({}.toString()); // toString , {} toString {}.toString.call([]); // [object Array] // jQuery jQuery.newMethod = function(){} jQuery.extend(obj); jQuery jQuery.fn = jQuery.fn.init.prototype = function(){} jQuery.fn.extend(obj); //extend var o1 = {name:1}; var o2 = {age:{sex:{hel:1}}}; jQuery.extend(o1,o2); console.log(o1.age === o2.age); : guid guid , var obj = { name: "John", test: function() { alert( this.name ); $("#test").unbind("click", obj.test); } }; $("#song").click( jQuery.proxy( obj, "test" ) ); // : ("#song").click( jQuery.proxy( obj.test, obj ) ); // $("#song").click( obj.test ); js jquery proxy // this function closure(fn, scope) { return function (){ fn.apply(scope, arguments); }; } // function invoke(obj, methodName) { return function(){ obj.methodName(obj, arguments); }; } var fns = [],conter = 0; // function addEvent(){ fn.guid = counter ++; fns[type] = fns[type] || {}; fns[type][fn.guid] = fn; o["on"+type] = function(){ // fns } } // function delEvent(o, type, fn) { delete fns[type][fn.guid]; } jquery :slideview slinkySlider anythingSlider

 
 

좋은 웹페이지 즐겨찾기