2013-01-07 215 views
-2

我需要在其他元素中通過ID獲取HTML元素。但我需要通過父元素來完成。通過其他元素通過ID獲取HTML元素

<div id="one" class="a">html value 1 
    <div id="two" class="a">html value 2</div> 
</div> 
+2

呃,什麼?你是否想通過點擊'#two'來獲得'#one'的'id'? –

+0

我需要通過具有ID ONE的元素來獲取具有ID TWO的元素。 – lugaru

+2

爲什麼?如果你知道這個ID,你就知道這個ID,並可以通過它來獲取元素。這裏不需要涉及其他元素。 – Quentin

回答

2
$('#one div:first').attr('id'); 
0

試試這個....

$('#one > div').attr('id'); 
0

使用$('#one #two')應該返回jQuery包裹元素div#two
注意id應該是唯一的

1

ID應該永遠是唯一的..用相同的ID兩個HTML元素是無效的..你可以用類和使用類選擇取代IDS ..

HTML

<div id="one" class="a">html value 1 
    <div class="a two">html value 2</div> 
    <div class="a three">html value 3</div> 
    <div class="a four">html value 4</div> 
    .... 
</div> 

JQUERY

$('#one.two').text(); //to get the text inside the element having class as two , gives(html value 2) here..