여러개의 클래스에서 선택된 요소의 index() 가 같은 값이 나 올 경우

아래  html 경우 버튼의 index() 을  얻고 싶은데 0 만나옴,,
이유는 같은  <div> 안에 형제 관계가 아니기 때문임 
<div class=”d-flex w-100″>
    <div><button type=”button” class=”_btnCustom btn w-50″ >btn1</button></div>
    <div><button type=”button” class=”_btnCustom btn w-50″ >btn1</button></div>
</div>

$(function(){
    $(“._btnCustom”).click(function()
   {
            alert($(this).index()); 
    })
})

아래와 같이 같은 div 안에 함께 있어야  원하는 index 조건이 성립됨
1개의 요소 div 안에 같은 형제 button 

<div class=”d-flex w-100″>
    <div>
            <button type=”button” class=”_btnCustom btn w-50″ >btn1</button>
            <button type=”button” class=”_btnCustom btn w-50″ >btn1</button>
    </div>
</div>

 

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다