纯javascript相当于jquery click()? [英] pure javascript equivalent of jquery click()?

查看:132
本文介绍了纯javascript相当于jquery click()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个捕捉鼠标点击的小应用程式。我在jquery写了原型,但是,因为它是一个小应用程序专注于速度,嵌入jquery只使用一个函数将是一个overkill。

I am building a small app which captures mouse clicks. I wrote the prototype in jquery but, since it is a small app focusing on speed, embedding jquery to use just one function would be an overkill.

我试图从修改此示例JavascriptKit

document.getElementById("alphanumeric").onkeypress=function(e){  
    //blah..blah..blah..  
}

但它没有工作时,我尝试

but it didn't work when I tried

document.getElementsByTagName("x").onclick

我做错了什么?

推荐答案

有一个p标签的列表,您想要捕获p标签的点击:

Say you have a list of p tags you would like to capture the click for the p tag:

var p = document.getElementsByTagName("p"); 
for(var i=0; i<p.length; i++){ 
 p[i].onclick = function(){ 
   alert("p is clicked and the id is " + this.id); 
 } 
}

请查看此处的示例以便更清楚:
http://jsbin.com/onaci/

Check out an example here for more clarity: http://jsbin.com/onaci/

这篇关于纯javascript相当于jquery click()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆