2015-07-11 71 views
0

EDIT你可以通過選定的div來獲取信息嗎?

按鈕嵌套在這樣的方式:通過

$(document).on("click" , ".down-arrow" , function() { 

}); 
<div> 
    <div> 
     <div class="this is the div I am after"> 
     <!-- To get this text --> 
    </div> 

    <div class="down-arrow" id="10"> 
     <!-- this is the div that has the arrow button --> 
     <img src="downarrow.png" id="10down" width="100%" height="auto"> 
     <img src="uparrow.png" style="display:none" class="upArrow" id="10up" width="100%" height="auto"> 
    </div> 
</div> 

該按鈕被觸發所以我呼應行和每一行都有一個唯一的標識符。

所選的按鈕位於div內,因此當我點擊按鈕時,我無法在包含該按鈕的div內獲得一段文本。

所以我用.outerHTML它顯示我以後的id,我怎麼才能專門選擇使用.outerHTML信息的按鈕外部的id?

var selectedDiv = document.getElementById(selectedId).outerHTML; 
+0

哪一行以及哪個後面的ID ..看起來像我的http://xyproblem.info – vinayakj

+0

您已標記jQuery,但您使用的是JavaScript。你是否也在使用jQuery? –

+2

按鈕在哪裏? – Varun

回答

1

如果您使用jQuery(我會假設,因爲你以爲你是),請嘗試以下操作:

$('.down-arrow').click({ 
    $(this).parent().children('.targetClass').text(); 
    // where targetClass is the class of whatever it is you want the text of 
}); 

如果你想走得更遠了DOM,增加更多的.parent()秒。

+0

@janicehoplin沒問題,樂意幫忙! –

相關問題