return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Get classList from event - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get classList from event - Stack Overflow

programmeradmin2浏览0评论

I have a simple JS event:

var changeAddress = document.getElementsByClassName('modal-open');

if (changeAddress.length) {
    var a = 0;
    while (a < changeAddress.length) {
        changeAddress[a].addEventListener('click', function () {
            console.log(this); // Here i want to get classList
            document.querySelector('.modal').removeAttribute('hidden');
        });
        a++;
    }
}

How can I get classList from this?

It's not necessary get it from this or throw classList. I just want to get all class names of element on which event is triggered. Thanks!

I have a simple JS event:

var changeAddress = document.getElementsByClassName('modal-open');

if (changeAddress.length) {
    var a = 0;
    while (a < changeAddress.length) {
        changeAddress[a].addEventListener('click', function () {
            console.log(this); // Here i want to get classList
            document.querySelector('.modal').removeAttribute('hidden');
        });
        a++;
    }
}

How can I get classList from this?

It's not necessary get it from this or throw classList. I just want to get all class names of element on which event is triggered. Thanks!

Share Improve this question edited May 17, 2016 at 13:26 AleshaOleg asked May 17, 2016 at 13:19 AleshaOlegAleshaOleg 2,2111 gold badge16 silver badges29 bronze badges 14
  • 3 If the code that you've written is in event handler, then you can directly use event.target.classList – Gangadhar Jannu Commented May 17, 2016 at 13:22
  • 1 The place you want to get the classList is outside of the event handler, so no event has been triggered at that point. All you have is an array of elements in changeAddress. – Rhumborl Commented May 17, 2016 at 13:23
  • 1 @AleshaOleg: So, what do you see from your console.log? Since it's inside the event, you should be able to do this.classList. – gen_Eric Commented May 17, 2016 at 13:27
  • 1 @GangadharJannu thanks! event.target.classList work! Could you please answer this question? I will mark it as answer – AleshaOleg Commented May 17, 2016 at 13:29
  • 1 @RocketHazmat it's undefined, but event.target.className works! – AleshaOleg Commented May 17, 2016 at 13:32
 |  Show 9 more ments

1 Answer 1

Reset to default 3

In the event handler callback you can get event as a parameter. With 'event' param we can get list of classes using either className or classList.

  1. Using classList: event.target.classList will directly returns list of class names in an Array.

  2. Using className: event.target.className will return string representation of class names with space as seperator. So you can use event.target.className.split("/\s") which will return array of class names.


We can use className or classList. className is supported by older browsers but modification (add, toggle, remove...) of classes is a tedious task, so classList is introduced and is easy to use.

However the classList property is not widely supported yet

Please refer Code with classList does not work in IE? for cross browser support

发布评论

评论列表(0)

  1. 暂无评论