简体   繁体   English

Javascript / jQuery - 如何获取clicked元素类的名称?

[英]Javascript/jQuery - How do I obtain name of the class of clicked element?

I googled and googled and I concluded that it's very hard to get answer on my own. 我用Google搜索并用谷歌搜索,我得出的结论是,我自己很难得到答案。

I am trying to use jquery or JavaScript to get a property of clicked element. 我正在尝试使用jquery或JavaScript来获取clicked元素的属性。 I can use "this.hash" for example - it returns hash value I presume. 我可以使用“this.hash”作为例子 - 它返回我认为的哈希值。

Now I would like to get name of the class of clicked element. 现在我想获得clicked元素类的名称。 Is it even possible? 它甚至可能吗? How? 怎么样? And where would I find this kind of information? 我在哪里可以找到这种信息?

  • jQuery documentation? jQuery文档? - All I can find is methods and plugins, no properties.. if its there - please provide me with link. - 所有我能找到的方法和插件,没有属性..如果它在那里 - 请提供链接。

  • JavaScript documentation? JavaScript文档? - is there even one comprehensive one? - 还有一个全面的吗? again please a link. 再请一个链接。

  • DOM documentation? DOM文档? - the one on W3C or where (link appreciated). - W3C或其中的一个(链接赞赏)。

And what is this.hash ? 这是什么。 this.hash - DOM JavaScript or jQuery? - DOM JavaScript还是jQuery?

In jQuery, if you attach a click event to all <div> tags (for example), you can get it's class like this: 在jQuery中,如果你将click事件附加到所有<div>标签(例如),你就可以得到它的类:

Example: http://jsfiddle.net/wpNST/ 示例: http //jsfiddle.net/wpNST/

$('div').click(function() {
    var theClass = this.className;  // "this" is the element clicked
    alert( theClass );
});

This uses jQuery's .click(fn) method to assign the handler, but access the className property directly from the DOM element that was clicked, which is represented by this . 这使用jQuery的.click(fn)方法来分配处理程序,但访问className直接从被点击的DOM元素,这是由表示属性this

There are jQuery methods that do this as well, like .attr() . 有一些jQuery方法也可以这样做, 比如.attr()

Example: http://jsfiddle.net/wpNST/1/ 示例: http //jsfiddle.net/wpNST/1/

$('div').click(function() {
    var theClass = $(this).attr('class');
    alert( theClass );
});

Here I wrapped the DOM element with a jQuery object so that it can use the methods made available by jQuery. 在这里,我用一个jQuery对象包装DOM元素,以便它可以使用jQuery提供的方法。 The .attr() method here gets the class that was set. 这里的.attr()方法获取已设置的类。

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. 我建议使用console.log(event)并使用Firebug / Developer工具将其转储到控制台中。

jQuery jQuery的

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

Javascript 使用Javascript

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

Try it out 试试看

http://jsfiddle.net/M2Wvp/ 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. 为了澄清,您不必为e.srcElement.className记录控制台以获得该类,希望这不会让任何人感到困惑。 It's meant to show that within the function, that will have the class name. 它意味着在函数中显示将具有类名。

$(document).click(function(e){
    var clickElement = e.target;  // get the dom element clicked.
    var elementClassName = e.target.className;  // get the classname of the element clicked
});

this supports on clicking anywhere of the page. 这支持单击页面的任何位置。 if the element you clicked doesn't have a class name, it will return null or empty string. 如果您单击的元素没有类名,则返回null或空字符串。

$('#ele').click(function() {
    alert($(this).attr('class'));
});

And here are all of the attribute functions. 以下是所有属性函数。

http://api.jquery.com/category/attributes/ http://api.jquery.com/category/attributes/

You can use element.className.split(/\\s+/); 你可以使用element.className.split(/ \\ s + /); to get you an array of class names, remember elements can have more than one class. 为了获得一个类名数组,请记住元素可以有多个类。

Then you can iterate all of them and find the one you want. 然后你可以迭代所有这些并找到你想要的那个。

window.onclick = function(e) {
    var classList = e.srcElement.className.split(/\s+/);
    for (i = 0; i < classList.length; i++) {
       if (classList[i] === 'someClass') {
         //do something
       }
    }
}

jQuery does not really help you here but if you must jQuery在这里并没有真正帮助你,但如果你必须的话

$(document).click(function(){
    var classList =$(this).attr('class').split(/\s+/);
    $.each( classList, function(index, item){
        if (item==='someClass') {
           //do something
        }
    });
});

There's a way to do this without coding. 没有编码就有办法做到这一点。 Just open the console of your browser (f12?) and go to element you want. 只需打开浏览器的控制台(f12?)并转到所需的元素即可。 After that, hover or click the item you want to track. 之后,悬停或单击要跟踪的项目。

Every change done on the DOM will be for a few seconds marked (or lightened) as another color on the console. 在DOM上进行的每个更改都将在控制台上标记(或点亮)作为另一种颜色的几秒钟。 (Watch the screen capture) (观看屏幕截图)

On the example, each time I hover a "colorItem", the 'div' parent and the "colorItem" class appears lightened. 在该示例中,每次我悬停“colorItem”时,'div'父级和“colorItem”类都会变亮。 So in this case the clicked class will be 'swiper-model-watch' or 'swiper-container' (class of the lightened div) 所以在这种情况下,点击的类将是'swiper-model-watch'或'swiper-container'(减轻div的类)

获取点击的名称类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取(和使用)在javascript / jQuery中被单击的元素? - How do I grab (and use) the element being clicked in javascript/jQuery? 如何获取包含Javascript / Jquery中单击文本的元素? - How do I get the element containing clicked text in Javascript/Jquery? 如何使用Jquery编写“如果未单击”或“如果在外部元素上单击”? - How do I write 'if not clicked' or 'if clicked outside element', using Jquery? Jquery:如何获取我点击的元素的当前id并更改它的名称? - Jquery : How do I get the current id of the element I have clicked on and change it’s name? 如何修复此JQuery函数以获得单击元素的父li元素的数据值? - How can I fix this JQuery function to obtain the data-value value of the parent li element of a clicked element? 如何使用其标签名称获取特定元素? - How do I obtain a particular element using its tag name? 获取点击类元素jquery的名称值 - getting name value of clicked on class element jquery 获取点击的元素ID或名称jquery或javascript - get clicked element ID or Name jquery or javascript jQuery:当它们共享相同的CSS类时,如何定位被点击的元素? - jQuery: How do I target the clicked element when they all share the same CSS class? 当单击孩子时,如何使用jQuery将类添加到父元素? - How do I use jQuery to add a class to a parent element when a child is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM