å¯ä»¥ä½¿ç¨hasclass()æ¹æ³æ¥å¤æï¼hasClass() æ¹æ³æ£æ¥è¢«éå
ç´ æ¯å¦å
å«æå®ç classï¼è¯æ³ä¸ºï¼$(selector).hasClass(class)
å·¥å
·åæï¼ç¼è¾å¨ãæµè§å¨
1ã设置ä¸ä¸ªæé®ç¹å»åå¤æä¸ä¸ªæ®µè½æ¯ä¸æ¯ææå®çç±»ï¼ç¸å
³ä»£ç é»è¾è§ä»£ç 注éï¼ä»£ç å¦ä¸ï¼
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){//è·åæé®å
ç´ å¹¶ç»å®ç¹å»äºä»¶
alert($("p:first").hasClass("intro"));//å¼¹åºå
ç´ påºç¨hasclassæ¹æ³çè¿åå¼ï¼å¦æ该å
ç´ åå¨introçç±»åå¼¹åºtrueå¦åfalse
});
});
</script>
<style type="text/css">
.intro
{
font-size:120%;
color:red;
}
</style>
</head>
<body>
<h1 id="h1">This is a heading</h1>
<p class="intro">This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>æ£æ¥ç¬¬ä¸ä¸ªæ®µè½æ¯å¦æ¥æç±» "intro"</button>
</body>
</html>
2ãä»ä»¥ä¸ä»£ç ä¸å¯ç¥ï¼ç¬¬ä¸ä¸ªå
ç´ pç¡®å®åå¨class为introçç±»åhasclassçè¿è¡ç»ææ¯trueï¼