2011-05-22 65 views
2

如何使用jquery獲取指向的項目。我的意思是指向項目,如果鼠標點擊正文中的某個div,我想獲取該div的信息,或者單擊選擇框或等等..所以他們沒有tchoasen由我,選定的項目都是網絡中的所有html元素,只是我想要接收我點擊的元素女巫的信息。使用jquery獲取指向元素

回答

0

嗯,我不知道如果我理解你的問題,但你指的.html()方法。下面的例子會在點擊或懸停時彈出div的內容。

<div id='test'>Content of div</div> 

//on click 
$('#.test').click(function(){ 
    alert($(this).html()); 
}); 

//on hover 
$('#.test').hover(function(){ 
    alert($(this).html()); 
}); 
0

假設你有一個div ID爲 '測試' 你的body元素中。通過jQuery註冊事件如下:

$("div#test").click(function(event){ 
    alert(event.target); 
    /*here event is the object fired, and target is the Div element i.e. source or you can use 'this' to refer to Div element*/ 
    alert(this); 
    // Here 'event.target' and 'this' will give the same Div element. 
}); 
+0

感謝安德魯,其實我不知道如何使用代碼窗口,新的到stackoverflow – user482144 2011-05-23 07:49:31