jquery 或者js 怎么获取页面光标所在的元素?

JavaScript06

jquery 或者js 怎么获取页面光标所在的元素?,第1张

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html,编写基础代码。

2、在index.html中的<script>标签,输入js代码:

var el = window.document.body

window.document.body.onmouseover = function(event) {

el = event.target

$('body').append('<br/>当前鼠标在' + $(el).html() + '元素上')

}

3、浏览器运行index.html页面,此时鼠标移动到123上,会打印出光标在123元素上。

用jquery库写就更简单呀,如这样:

$(function(){

    $('div#libox li').mouseover(function(){alert($(this).find('span').html())})  

})