jQuery로 일반 ID 참조를 만드는 방법
-
05-07-2019 - |
문제
같은 클래스를 가진 약 6 개의 div 요소가 있습니다. 내가 그들 중 하나를 마우스로드 할 때 나는 그들 옆에 nother div를 보여주고 싶습니다.
나는 그들에게 mydiv-가 항상 일정하다는 양식 id = "mydiv-divname"의 ID를 모두 주려고 생각하고있다.
mydiv-* 요소를 어떻게 참조 할 것인가? 나는 정확한 구문을 찾을 수 없지만 *는 $ ( "#mydiv-[ *])와 같은 것이되어야한다고 생각합니다. 여기서 *는 일종의 와일드 카드를 나타냅니다.
해결책
신분증은 어떤 목적을 제공합니까? 모두 동일한 클래스 이름으로 태그가있는 경우 클래스별로 모두 액세스 할 수 있습니다.
`$(".className")...
해당 요소 중 하나가 호버링 될 때 이벤트를 트리거하려면 사용하십시오.
`$(".className").hover(... )
Hover () 내의 함수는 실제로 호버링되는 요소에 대해서만 트리거됩니다.
그들은 당신이 달성하려는 것과 비슷한 일을합니다. 여기 - 호버에서 하나의 요소를 찾거나 아웃하는 (해당 클래스로 표시된 페이지의 많은 요소 중 하나)
다른 팁
왜 ID 대신 선택기에서 클래스를 사용할 수없는 이유는 무엇입니까?
jQuery('.commonClass');
당신이 다음과 같은 것을 위해가는 것 같습니다.
HTML :
<div class="content" id="con_a">Hello world.</div>
<div id="show_con_a" style="display:none">Show Me on content div "a" hover</div>
<div class="content" id="con_b">Nice to meet you.</div>
<div id="show_con_b" style="display:none">Show Me on content div "b" hover</div>
<div class="content" id="con_c">See you later.</div>
<div id="show_con_c" style="display:none">Show Me content div "c" hover</div>
자바 스크립트 :
//Collect all divs with 'content' class
$('.content').each(function(){
//Set mouse handler for each content div
$(this).hover(function(){
$('#show_' + this.id).show();
},
function(){
$('#show_' + this.id).hide();
});
});
대체 자바 스크립트 :
//Collect all divs with an id that begins with 'con_'
$("[id=^'con_']").each(function(){
//Set mouse handler for each content div
$(this).hover(function(){
$('#show_' + this.id).show();
},
function(){
$('#show_' + this.id).hide();
});
});
제휴하지 않습니다 StackOverflow