如何获取触发“onclick”的元素事件
How to get the element that fired the "onclick" event
<script>
function clicky(e){
console.log(e) //the clicked element
}
</script>
<span onClick="clicky(this)">Clickable</span>
在上面的脚本中,console.log(e)
将给我点击的<span>
。是否有任何方法,我可以省略clicky(
this
)
,仍然得到元素?
这是因为我不想把(this)
涂满整个文档。
见:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<div id="foo" style="background:blue; width:100px; height:100px">
<script>
function clicky(e){
console.log(e);
}
var foo = document.getElementById("foo");
foo.onclick = function(e){clicky((e || window.event).target);}
</script>
</body>
</html>
你可以试试这个,但没有测试过。
var spans = document.getElementsByTagName('span');
spans.attachEvent('click'.'clicky');
function clicky(e){
console.log(e) //the clicked element
}
或
var spans = document.getElementsByTagName('span');
for (i in spans)
{
spans[i].attachEvent('click'.'clicky');
}
function clicky(e){
console.log(e) //the clicked element
}
function clicky(e, elem){
<span onClick="clicky(event, this)">Clickable</span>
或者您可以使用Prototype或jQuery或任何其他库。我会改善你的生活。
相关文章:
- 如何在执行此特定onclick事件时执行JavaScript函数
- 如何调用“;链接_;在onclick事件上使用Javascript
- 如何使用jquery迭代具有相同属性的html元素并查找onclick事件
- 选择onclick事件jquery
- 执行php函数的onclick事件的其他替代方案
- 动态创建OnClick事件Javascript
- onClick事件未触发reactjs
- 使用java脚本更改onClick事件中菜单选项卡的颜色
- 如何使用OnClick事件通过参数调用来调用PHP函数
- 使用jQuery动态添加onClick事件
- onclick事件不'不要在萤火虫或铬中点火
- 如何向onClick事件处理程序传递一个接受参数的函数,并且仍然将该函数绑定到组件's”;这个“;上下文
- xPages标签onclick事件不'不要在空白处工作
- 用beautifulsoup-python调用onclick事件
- 使用JavaScript将onclick事件与参数绑定
- 使用onClick事件扩展Aviarc容器小部件
- 在按钮的onclick事件上将字符串传递给JavaScriptFuntion
- 用于显示onclick事件计数的可调整跨度标记
- Javascript onclick事件在第一次单击时未启动
- 如何使用onclick事件触发此代码.我尝试过,但失败了