2013-10-07 131 views
0

div容器很適合打開溢出內容。 它的工作原理。 但是,如何將觸發的HTML元素的引用與jQuery連接起來。 應該有比這$("#"+divId)如何獲取jQuery使用的HTML元素的對象引用?

我得到這個代碼更好的辦法:

$('.text').mouseenter(function() { 

//var $id = $(this); ? 
//var $id = document.getElementById(divId); ? 
var divId = this.id; 
    var $id = $("#"+divId); 

var overflow = $id.prop("scrollHeight"); 

this.divHeight = $id.height(); 

$id.animate({height:overflow},1000); 
}); 

.text{ 
color: #000000; 
background-color: #ffffff; 
overflow: hidden; 
height: 100px; 
position: relativ;} 


<div class="text" id="text1"> 
<content> 
</div> 
<div class="text" id="text2"> 
<content> 
</div> 
<div class="text" id="text3"> 
<content> 
</div> 

PS爲什麼$(this).height()工作,但這不是$(this).prop("scrollHeight")

感謝您的幫助理解。

+0

所以你想獲得'this'作爲一個jQuery集合吧? '$ this = $(this);'...更多細節在這裏'=>'[api.jquery](http://api.jquery.com/jQuery/) – Stphane

+0

沒有收藏,只是$的事件引用觸發div元素。所以我可以使用它作爲元素的選擇器(例如$(「#text1」)) – Pek

+0

您可以隨意調用它,實際上它是對具有「mouseenter」偵聽器的div的引用。@ Pek – Stphane

回答

2

嘗試這樣的事情

改變這種

var divId = this.id; 
var $id = $("#"+divId); 

TO

var $id = $(this); 
+0

適用於$(this).height(),但不適用於$(this).prop(「scrollHeight」)。 – Pek

+0

你在scrollHeight上得到了什麼 –

0
Use jquery method like this 
    $(this).prop("scrollHeight"); 
相關問題