복붙노트

[JQUERY] jQuery를 선택에서 "컨텍스트"는 무엇입니까?

JQUERY

jQuery를 선택에서 "컨텍스트"는 무엇입니까?

해결법


  1. 1.이 차이이며, 다른 사람들이 생각대로되지 미묘하다.

    이 차이이며, 다른 사람들이 생각대로되지 미묘하다.

    편집 : 각의 뜻하는 예 :

    이 선택 어떤 실망의 휴식.

    먼저 우리가 가진 : 상황에 맞는 선택을 http://api.jquery.com/jQuery/#jQuery-selector-context

    $('input.current_title', '#storePreferences').prop('disabled', false);
    

    이 말한다 : 컨텍스트에서 선택기를 사용합니다. http://api.jquery.com/jQuery/#jQuery-selector-context

    이 양식 일 수도 있지만, 정말해야합니다 :

    $('input.current_title', $('#storePreferences')).prop('disabled', false);
    

    또는

    var myContext = $('#storePreferences');
    $('input.current_title', myContext).prop('disabled', false);
    

    "A DOM 요소, 문서, 또는 jQuery를 컨텍스트로 사용"이 상황에 맞는 선택에 대한 요구 사항이 충족 충족합니다.

    이 말한다 : 컨텍스트를 사용하여, 내부 찾아 선택 것이다. 동등한은 다음과 같습니다

    $('#storePreferences').find('input.current_title').prop('disabled', false);
    

    어떤 내부적으로 발생하는 것입니다. '#storePreferences'을 찾아 그 모든 'input.current_title'일치하는 요소를 찾을 수 있습니다.

    그럼 우리가 가지고 후손 선택기

    $('#storePreferences input.current_title').prop('disabled', false);
    

    #storePreferences 요소 내부의 모든 input.current_title 요소를 찾을 : 이것은 후손 선택기 말한다 ( "조상 후손") http://api.jquery.com/descendant-selector/이다. IT는 까다로운 GETS 곳이다! - 그게 정확히 그것이 무엇입니다 -

    발견 ALL을 input.current_title은 (어디서나), 다음 해당 INSIDE에게 #storePreferences 요소를 찾습니다.

    따라서, 우리는 왼쪽 선택에 jQuerys '지글 지글 오른쪽으로 실행 - 그것은 처음 MORE (잠재적) 발견, 그래서 그것이 성능 저하 / 문제가 될 수있는 필요 이상으로.

    따라서, 형태 :

    $('#storePreferences').find('input.current_title').prop('disabled', false);
    

    가능성이 가장 높은 후손 버전보다 더 잘 수행한다.


  2. 2.차이는 요소를 선택하는 방법이다.

    차이는 요소를 선택하는 방법이다.

    $('input.current_title', '#storePreferences');
    

    1과 동일하다 :

    $('#storePreferences').find('input.current_title');
    

    하지만 해당하지 않는 것입니다 :

    $('#storePreferences input.current_title');
    

    심지어 같은 요소가 영향을받습니다하지만.

    그들은 같은하지 않은 이유는 끝이 호출 될 때 컨텍스트가 #storePreferences에 반환하는 찾기를 사용하여 수 있다는 것입니다.

    // HANDLE: $(expr, $(...))
    } else if ( !context || context.jquery ) {
        return ( context || rootjQuery ).find( selector );
    
    // HANDLE: $(expr, context)
    // (which is just equivalent to: $(context).find(expr)
    } else {
        return this.constructor( context ).find( selector );
    }
    

  3. 3.귀하의 예제에서 나는 약간의 차이가 있다고 생각합니다.

    귀하의 예제에서 나는 약간의 차이가 있다고 생각합니다.

    그것은 당신이 특정의 DOM 요소에 여러 요소를 선택 시작보다 효율적으로 사용하게된다.

    // Get the div in the body with the id of storePreferences
    var sp = $('body div#storePreferences');
    
    
    // jQquery will only look for **input.current_title** **input.name** **input.age** in side **sp** div in the DOM.
    // Faster
    $('input.current_title', sp).prop('disabled', false);
    $('input.name', sp).prop('disabled', false);
    $('input.age', sp).prop('disabled', false);
    
    
    
    
    // jQquery will look for **input.current_title** in entire DOM
    // Slower
    $('#storePreferences input.current_title').prop('disabled', false);
    
  4. from https://stackoverflow.com/questions/16422478/what-is-context-in-jquery-selector by cc-by-sa and MIT license