Javascript / jQuery - 如何获取点击元素的类名? [英] Javascript/jQuery - How do I obtain name of the class of clicked element?

查看:2006
本文介绍了Javascript / jQuery - 如何获取点击元素的类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试使用jquery或JavaScript来获取点击的属性元件。我可以使用this.hash,例如 - 它返回我假设的哈希值。



现在我想获取点击元素的类名。
甚至有可能吗?怎么样?我在哪里可以找到这种信息?




  • jQuery文档? - 所有我可以找到的是方法和插件,没有属性..如果它在那里 - 请提供我的链接。


  • JavaScript文档? - 甚至有一个综合的?再次请一个链接。


  • DOM文档? - 一个在W3C或其中(链接赞赏)。




这是什么是散列? - DOM JavaScript或jQuery?

解决方案

此示例将适用于页面中的每个元素。我建议使用 console.log(event),并使用Firebug / Developer工具将其转储到控制台中。



jQuery



  $(window).click(function(e){
console.log (e); //然后e.srcElement.className有类
});



Javascript



  window.onclick = function(e){
console.log(e); // then e.srcElement.className有类
}



尝试出来



http://jsfiddle.net/M2Wvp/



编辑

要澄清,您不必为 e .srcElement.className 有类,希望这不会混淆任何人。这意味着在该函数中显示该类名。


I googled and googled and I concluded that it's very hard to get answer on my own.

I am trying to use jquery or JavaScript to get a property of clicked element. I can use "this.hash" for example - it returns hash value I presume.

Now I would like to get name of the class of clicked element. Is it even possible? How? And where would I find this kind of information?

  • jQuery documentation? - All I can find is methods and plugins, no properties.. if its there - please provide me with link.

  • JavaScript documentation? - is there even one comprehensive one? again please a link.

  • DOM documentation? - the one on W3C or where (link appreciated).

And what is this.hash? - DOM JavaScript or jQuery?

解决方案

This example will work on every element in the page. I'd recommend using console.log(event) and poking around at what it dumps into your console with Firebug/Developer tools.

jQuery

​$(window).click(function(e) {
    console.log(e); // then e.srcElement.className has the class
});​​​​

Javascript

window.onclick = function(e) {
    console.log(e); // then e.srcElement.className has the class
}​

Try it out

http://jsfiddle.net/M2Wvp/

Edit
For clarification, you don't have to log console for the e.srcElement.className to have the class, hopefully that doesn't confuse anyone. It's meant to show that within the function, that will have the class name.

这篇关于Javascript / jQuery - 如何获取点击元素的类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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