2011-09-11 4 views
2

에서 볼 코드의이 이상한 조각은이 같은 간단한 함수와 변수의 단지 무리입니다 :나는 항상 내가 자바 스크립트 전문가가 아니지만, 기존의 자바 스크립트가 내가 알고있는 자바 스크립트

function doSomething(){ 
    var data = "test"; 
    return data; 
} 

그러나 최근에 난 다음과 같은 자바 스크립트 코드를 볼 수 있습니다 :

$(document).ready(function(){ 
        $("#about").hide(); 
        $(".tababout").collapser({ 
         target: '#about', 
         effect: 'slide', 
         changeText: false, 
        }); 
     }); 

대체 무엇입니까? 뭐라고 해요? 모든 브라우저에서 더 쉽고 지원됩니까? 제발 좀 더 자세한 정보가 필요해.

+1

JohnD, 이것은 * 가장 가능성이 라이브러리 *의 jQuery를입니다 말했듯이. 그러나 그것은 여전히 ​​자바 스크립트이므로이 구문이 이상하게 보일 경우 JavaScript를 더 깊이 파헤쳐 야합니다. [MDN에서 제공하는 매우 포괄적 인 안내서] (https://developer.mozilla.org/en/JavaScript/Guide). –

답변

5

당신이 찾고있는 것은 jQuery입니다. 외부 라이브러리이지만 most (모두는 아닐 경우) 브라우저를 지원합니다. 그래도 사용하려면 프로젝트를 다운로드하여 포함해야합니다 (Downloading jQuery 참조).

+0

은 모든 브라우저에서 지원됩니까? – Reacen

+0

업데이트 된 답변보기 – JohnD

+0

그들은 IE 6.0+, FF 2.0+, Safari 3.0+, Opera 9.0+, Chrome을 요구합니다. http://docs.jquery.com/Browser_Compatibility –

1

그냥 평범한 구식 자바 스크립트입니다 :

var $ = function(sel) { 
    return new init(sel); 
}; 

function init(sel) { 
    if (sel.nodeName) { 
     this[0] = sel; 
     this.length = 1; 
    } else { 
     var elems = document.querySelectorAll(sel); 
     for (var i = 0; i < elems.length; i++) { 
      this[i] = elems[i]; 
     } 
     this.length = elems.length; 
    } 
    return this; 
}; 
init.prototype.ready = function(fn) { 
    _ready(fn); 
    return this; 
}; 
function _ready(fn) { 
    if (!document.body) { 
     setTimeout(function(){_ready(fn);}, 0); 
    } else { 
     fn(); 
    } 
} 
init.prototype.hide = function() { 
    this.each(function() { 
     this.style.display = 'none'; 
    }); 
    return this; 
}; 
init.prototype.show = function() { 
    this.each(function() { 
     this.style.display = 'block'; 
    }); 
    return this; 
}; 
init.prototype.each = function(fn) { 
    for (var i = 0; i < this.length; i++) { 
     fn.call(this[i], i, this[i]); 
    } 
    return this; 
}; 

$("#about").hide().each(function(i,el) { 

    setTimeout(function(){$(el).show()}, 2000); 

}); 

DEMO :http://jsfiddle.net/JGWUs/3/

관련 문제