hasClass() 函数
判断当前选择元素是否有指定的类名
语法
$(selector).hasclass(classname)
返回值
返回值为布尔型(true/false)
如果当前选择对象包含多个元素,只要其中任意一个元素包含该类名,则返回true
实例说明
代码
<!DOCTYPE html>
<html lang="zh-CN">
<head lang="en">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> </head>
<body>
<div></div>
<div id="div"></div>
<div>
<ul>
<li>one</li>
<li id="two" class="li">two</li>
<li>three</li>
</ul>
</div>
<script>
document.writeln( $("#two").hasClass("li") );//返回true
document.writeln( $("ul").hasClass("li") );//返回false
document.writeln( $("#div").hasClass("li"));//返回false 因为id=div对象中没有该类名
</script>
</body>
</html>
运行结果
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJgAAAB/CAIAAACc4ZxMAAAETUlEQVR4nO2dPXKzMBCGVeYQKXIDmpQ5StTnGGl1ljSaySFcuGXGZY6RUl+BjVaglQ35GK9f3qdJzK/MYwG2dheXCATu3g0g/weKBIEiQaBIECgSBIoEgSJBoEgQKBIEigSBIkGgSBAoEgSKBIEiQaBIECgShM1Fnk6nw+Hw+/u79Y52zrYi39/fnXPOuefn5+Px2F44enemC31KKaU+dM45H4e/YoaY65xzzscN38RjsKHIr68vJ3h9fW0sHH3W0Ydu/D/67E9O70MnrEZfKN4jG4r8/PyUIp+entRFSy3ydfSiu+UXojeyU6aUNhX5/f0tj/Tb25u6aPTzs+ZgRhFZTCZp62vkx8fHYPHl5eV0OqnLremRuz+Zlmx+1/rz83M8Hq/etco+OblG1kSmPnSiE/ch7L1/Gvoeme9apa1xwmX2qC8v79g7LYkkf4EiQaBIECgSBIoEgSJBoEgQKBIEigSBIkGgSBBsiYyBP5quxFLMDsem/oCRmB0xkpHHP84TfRz+6UIcRkPy4DIHP0asxOykVOuRfegugQKXgeYwDjhXY3x2i42YnYHKqfVschxGPnvUIwp2i42YnYGajqErxuB956Psj0qMz26xEbMzUO1X0XchhND3ofPhEtHBHjnDSsxOSkW/ilEG6oyhHk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