use event.target to get the class value?

后端 未结 5 1279
你的背包
你的背包 2021-02-02 18:13

i have a DOM element with class=\'tag\'.

i want to check if the class value is tag and alert a message if it\'s true.

i wrote:

    $(\"#thread\")         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 18:23

    At first, the event argument work only for event handlers, and you are using the $.each method.

    I don't really think that you want/need to use the $.each method, since you are using an #id selector, and id's should be unique.

    To check if an element contains an specific class you can use the hasClass method:

    if ($("#thread").hasClass('tag')) {
      //...
    }
    

    Also if you have a DOM element, to get it's class attribute, you should access it with className instead class, that's because class is a future reserved word in JavaScript, the same thing happens with other attributes like for, should be accessed as htmlFor...

提交回复
热议问题