---
title: "JQuery类选择器只对原始类名元素起作用"
author: "robinjoe"
date: "2010-09-11T09:29:47Z"
tags: []
categories: ["Web前端"]
slug: "jquery"
draft: false
lastmod: "2010-09-11T09:29:47Z"
id: 760750
url: "https://robinjoe.iteye.com/blog/760750"
---

JQuery类选择器只对原始类名元素起作用，对后添加的class不起作用。

html代码：

```java
 
<body>
  <h3 align="center">joeking</h3>
  <hr>
  <p>p1</p><br>
  <p id="p">p2</p><br>
  <p>p3</p>
  <p class="red">p4</p>
 </body>

```

js：

```java

$(document).ready(function(){
	$("p").click(function(){
		$("p").addClass("red");
	});

	$("#p").click(function(){
		$(this).removeClass("red");
	});

	$(".red").click(function(){
		alert("this is red");
	});
});

```

css：

```java

.red{color:red}
p{text-align:center}

```


代码如上，只有单击p4标签时，才会弹出alert对话框。
使用火狐firebug查看html，所有的p元素都添加了class，但是为什么不起作用呢？