使用jquery获取指向元素

时间:2011-05-22 15:44:00

标签: javascript jquery html

如何使用jquery获取指向项目。我的意思是指向项目,如果鼠标单击正文中的某个div,我想获取该div的信息,或者单击选择框等等。所以它们是我没有tchoasen,所选项目都是网页中的html元素,只是我想收到我点击过的元素信息。

4 个答案:

答案 0 :(得分:2)

$(document.body).click(function(ev) {
    $(ev.target); // is the clicked element
});

Live Example

ev.target .click

答案 1 :(得分:0)

答案 2 :(得分: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());
});

答案 3 :(得分:0)

假设您的div元素中有body个id'test'。通过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.
});