javascriptã§è¦ç´ ã«ã¤ããã¯ã©ã¹ã®1ã¤ãã ãåãåºãããæã
element.className.split(" ")[0];
ã§æåã®ã¯ã©ã¹åãåå¾ã§ãã¾ãã
ããã¦ãjQueryã使ã£ã¦ããæãã®ã§ã
// HTML <p id="hoge" class="className1 className2">è¦ç´ ã ã</p>
ã
// jQuery $('#hoge').attr('class').split(" ")[0];
ã®æ§ãªè¨è¿°ããããã¨ãå¤ãããããªããã¨æãã¾ãã
ããããã§ãã©ããã§ãã
jQueryã®attr
ã使ãã¨ã
ä¸ã®ããã«è¦ç´ ã«ã¯ã©ã¹ãç¡ãæã«ã ã¨splitã§ããªãããã¨ã¨ã©ã¼ãèµ·ãã¦ãã¾ãã¾ãã
// HTML <p id="hoge">è¦ç´ ã ã</p>
ã
// jQuery $('#hoge').attr('class').split(" ")[0]; // _人人人人人人_ // ï¼ãçªç¶ã®æ»ãï¼ // ï¿£Y^Y^Y^Y^Yï¿£
ããã¯ãã¯ã©ã¹åãåå¨ããªãæattr('class')
ãundefined
ãè¿ãã®ãåå ã£ã½ãã§ãã
classNameã ã¨ã¯ã©ã¹åãç¡ãã¦ã''
ã¨ç©ºæååãããã®ã§splitã§ã¨ã©ã¼ãèµ·ããªãã£ã½ãï¼
$(element).get(0)
ã¨ã$(element)[0]
ã¨ããã¦ããã¨è¯ãããã§ãã
// HTML <p id="hoge">è¦ç´ ã ã</p>
ã
// jQuery $('#hoge').get(0).className.split(" ")[0]; // ã¾ã㯠$('#hoge')[0].className.split(" ")[0];
className
javascriptã§è¦ç´ ã«ã¤ããã¯ã©ã¹ã®1ã¤ãã ãåãåºãããæã¯
element.className.split(" ")[0];
jQueryã使ã£ã¦ãæã¯
$element.get(0).className.split(" ")[0]; // ã¾ã㯠$element[0].className.split(" ")[0];
ã¨ããã°è¯ãã£ã½ãï¼
追è¨( 2014 11.11 )
JSã§1ã¤ãã®ã¯ã©ã¹åãåå¾ããã - ããã¡ã¢ http://t.co/1yjLgZX3J0
æè¿ã®ãã©ã¦ã¶ãªããelement.classList[0]ã§åå¾ã§ãã
— amano (@amano225) 2014, 11æ 8
æè¿ã®ãã©ã¦ã¶ãªãã
element.classList[0]
ã§åå¾ã§ãã ã£ã½ãï¼â» 試ãã¦ãªã