2011-05-22 39 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