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

JavaScript013

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元素上。

定义如下JS函数,然后DIV触发onclick

事件就OK。

function

CopyUrl()

{

var

htm

=

document.getElementById("yourdiv").innerHTML

window.clipboardData.setData('text',

htm)

}

yourdiv

就是你的div的ID