2014-09-03 100 views
0
<!DOCTYPE html> 
<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $("button").click(function(){ 
    var value = $("p").removeClass("intro"); 

    }); 
}); 
</script> 
<style> 
.intro 
{ 
font-size:120%; 
color:red; 
} 
</style> 
</head> 
<body> 

<h1>This is a heading</h1> 
<p class="intro">This is a paragraph.</p> 
<p class="intro">This is another paragraph.</p> 

<button>Remove the "intro" class from all p elements</button> 

</body> 
</html> 

嗨,我们如何知道removeClass操作是成功还是失败?

这是我删除某些类

我的问题的程序是,我怎么能知道,如果removeclass操作是成功还是失败?

$("p").removeClass("intro");可以为此设置任何返回类型?

+1

你可以使用'$(“p.intro”)。length' – Satpal 2014-09-03 09:56:20

+0

你可以使用'.hasClass'函数。例如'$(“p”)。hasClass(“intro”);' – 2014-09-03 09:57:25

+1

你可以使用hasClass() – jtromans 2014-09-03 09:57:35

回答

6

您可以使用.hasClass()验证:

$("p").hasClass("intro") 
1

您可以验证该节点是否仍具有类你执行了它.removeClass()后与.hasClass()功能:

if ($('p').hasClass('intro')) { 
    ... 
} 
0
<script> 
$(document).ready(function(){ 
    $("button").click(function(){ 
    $("p").removeClass("intro"); 
    if ($("p").hasClass("intro")) 
    { 
    alert("class not removed !"); 
    } 

    }); 
}); 
</script> 
相关问题