2012-03-17 5 views
7

중첩 된 jQuery 선택기가 있어야합니까?중첩 된 jQuery 선택기

예를 들어

: 페이지는 또한 상에 ID = "LeadEditForm_Title" 곳이있다

경우 다음 않습니다 ...

jQuery("[id='A0.R0.Main Phone Number']").live('mousedown',function(e) { 
    var container = $(this).width(); 
    var width_offset = -50; 
    var top_offset = 25; 
    var width = (container + width_offset).toString(); 
    jQuery(".mywidget").appendTo(document.body).css('width', width + 'px'); 
    jQuery(".mywidget").appendTo(document.body).css('position', 'absolute'); 
    jQuery(".mywidget").appendTo(document.body).css('top', ($(this).offset().top+top_offset).toString() + 'px'); 
    jQuery(".mywidget").appendTo(document.body).css('left', Math.round($(this).offset().left) + 'px'); 
}); 

질문 :

나는 어떻게해야합니까 jQuery(".LeadEditForm_Title") 다음에 위의 경우합니까? 기본적으로 중첩 된 jQuery를 호출 ... 나는 그들이 가지고 예를 본 적이 :

jQuery(function(){ // my code goes here }); 

을하지만 나는 그것이 ID ".LeadEditForm_Title"에 의존합니다. 둥지 셀렉터

+0

지금까지를 내가 알 수 있듯이, 그 함수는 괜찮아 보인다. –

+2

ID에 실제로 공백이 있습니까? HTML 사양에 따르면 ID에는 공백이 없어야합니다. 일반적으로 ID는'# '으로 선택합니다. 나는. '$ ("# myID")'. –

+0

왜'mousedown'이 아닌'click'입니까? 또한 id는 그와 같은 공백이 없어야합니다. 유효성이 의심 스럽습니다. 대신에 '데이터'를 사용할 수 있습니다 ... – elclanrs

답변

22

, 당신은 find을 사용

jQuery('#mySelectorId').find('.mySelectorClass') 

이뿐만 아니라이 일을하는 것과 같습니다

jQuery('#mySelectorId .mySelectorClass') 

사이에 공백을 넣어 확인된다. 공간이 없으면 해당 ID가 이고 해당 클래스가 인 요소를 선택합니다.

은 또한 코드가 아마 당신이 생각하는 일을하지 않는 점에 유의합니다 :

jQuery("[id='A0.R0.Main Phone Number']").live('mousedown',function(e) { 
    var container = $(this).width(); 
    var width_offset = -50; 
    var top_offset = 25; 
    var width = (container + width_offset).toString(); 
    jQuery(".mywidget").appendTo(document.body).css('width', width + 'px'); 
    jQuery(".mywidget").appendTo(document.body).css('position', 'absolute'); 
    jQuery(".mywidget").appendTo(document.body).css('top', ($(this).offset().top+top_offset).toString() + 'px'); 
    jQuery(".mywidget").appendTo(document.body).css('left', Math.round($(this).offset().left) + 'px'); 
}); 

마지막 4 개 jQuery(".mywidget") 통화가 몸에 때마다 위젯을 추가하고 있습니다. 이 밖에,

jQuery("[id='A0.R0.Main Phone Number']").live('mousedown',function(e) { 
    var container = $(this).width(); 
    var width_offset = -50; 
    var top_offset = 25; 
    var width = (container + width_offset).toString(); 
    jQuery(".mywidget").appendTo(document.body).css({ 
     width: width + 'px', 
     position: 'absolute', 
     top: ($(this).offset().top+top_offset).toString() + 'px', 
     left: Math.round($(this).offset().left) + 'px'; 
    }); 
}); 

당신의 ID를 :

jQuery("[id='A0.R0.Main Phone Number']").live('mousedown',function(e) { 
    var container = $(this).width(); 
    var width_offset = -50; 
    var top_offset = 25; 
    var width = (container + width_offset).toString(); 
    jQuery(".mywidget").appendTo(document.body).css('width', width + 'px').css('position', 'absolute').css('top', ($(this).offset().top+top_offset).toString() + 'px').css('left', Math.round($(this).offset().left) + 'px'); 
}); 

하나 CSS 호출로 감소 될 수있다 : 당신은 정말 각 스타일에 대한 CSS를 한 번에 추가하고 변경하려면 HTML 사양에 따르면 공백이 없어야합니다. 유효한 ID를 가지고 있다면, 당신은 다음과 같이 선택합니다 :

jQuery("#A0.R0.Main_Phone_Number") 
+0

어쨌든 이것을 할 수 있다면 if (jQuery ("# A0.R0.Main_Phone_Number")) then (다른 jQuery 호출을 수행하십시오) ... – htmlfarmer

+0

"다른 호출"이란 무엇을 의미합니까? 수행하려고하는 것은 무엇입니까? –

+0

html이 무엇을하는지 설명하면 도움이됩니다. –

-4

을 중첩 jQuery를 사용하면 다음과 같이 그것을 할 수 있습니다 전화하고 싶은 경우 ...

$(document).ready(function(){ 
    $(document).ready(function(){ 
    // javascript code here... 
    }); 
}); 
+1

이것은 부적절한 대답입니다. – Jason

+0

@ Jason 나 자신처럼 정보가없는 이유를 설명 할 수 있습니까? – Eoin