开发者

How to use jquery Attr() to select only first attribute?

开发者 https://www.devze.com 2022-12-19 16:12 出处:网络
I have a div as follows: <DIV CLASS=\"variable productPopup\"></开发者_开发百科DIV>

I have a div as follows:

<DIV CLASS="variable productPopup"></开发者_开发百科DIV>

When assigning it to a var, how do I only select variable? Please note that variable changes, so it needs to select the first item, not literally the words "variable"

var ID = $(this).attr("class");


To get the first class mentioned, you can split the value by spaces:

var firstClass = $(this).attr("class").split(" ")[0];

So if the class attribute was "foo Bar", you'd get "foo".

0

精彩评论

暂无评论...
验证码 换一张
取 消