반응형
jQuery: 부모, 부모 ID를 가져오시겠습니까?
<ul id ="myList">
<li><a href="www.example.com">link</a></li>
</ul>
jQuery를 사용하여 ul(myList)의 ID를 얻으려면 어떻게 해야 합니까?링크를 클릭하면 내 j-script 이벤트가 트리거됩니다.시도해 본 결과:
$(this).parent().attr('id');
하지만 그것은 리의 아이디를 얻었고, 저는 한 단계 더 올라가야 하고, 클릭도 리에 붙일 수 없습니다.
$(this).parent().parent().attr('id');
부모님의 신분증을 얻는 방법입니다.
편집:
$(this).closest('ul').attr('id');
귀하의 경우에 더욱 완벽한 솔루션입니다.
$(this).closest('ul').attr('id');
다음은 3가지 예입니다.
$(document).on('click', 'ul li a', function (e) {
e.preventDefault();
var example1 = $(this).parents('ul:first').attr('id');
$('#results').append('<p>Result from example 1: <strong>' + example1 + '</strong></p>');
var example2 = $(this).parents('ul:eq(0)').attr('id');
$('#results').append('<p>Result from example 2: <strong>' + example2 + '</strong></p>');
var example3 = $(this).closest('ul').attr('id');
$('#results').append('<p>Result from example 3: <strong>' + example3 + '</strong></p>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id ="myList">
<li><a href="www.example.com">Click here</a></li>
</ul>
<div id="results">
<h1>Results:</h1>
</div>
도움이 되었는지 알려주세요.
언급URL : https://stackoverflow.com/questions/10260667/jquery-get-parent-parent-id
반응형
'itsource' 카테고리의 다른 글
C#에서 문자열의 새 줄을 잘라내는 가장 쉬운 방법은 무엇입니까? (0) | 2023.09.04 |
---|---|
보기에서 CURRENT_ROLE 사용 (0) | 2023.09.04 |
동일한 데이터 유형에도 불구하고 "외부 키 제약 조건이 잘못 형성됨" (0) | 2023.09.04 |
SQL 오류: ORA-02298: 유효성을 검사할 수 없습니다(SYSTEM).AEROPUTO_FK) - 상위 키를 찾을 수 없음 (0) | 2023.09.04 |
C#에서 어제 날짜를 가져오는 방법 (0) | 2023.09.04 |