87,907
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<style>
.test{color:red;}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="first"></div>
<div></div>
<div></div>
<div></div>
<script>
$("<p>Hello world!</P>").appendTo("div").end().addClass("test");
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>H</title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<script type="text/javascript">
$(function(){
var p = $("<P>my-jquery</P>");
p.appendTo($("div"));
p.html("Hello world");
});
</script>
</body>
</html>
.appendTo("div")时就已经给所有的div增加了之前定义的元素对象
.end()返回上一次的选择对象,记住,是对象本身,也就是$("<p>Hello world!</P>")
.addClass("test");//给它添加一个test样式
$("<p>Hello world!</P>").addClass("test").appendTo("div");
<div id="first">
<div></div>
<div></div>
<div></div>
</div>
<script>
$("<p>Hello world!</P>").appendTo($("#first div")).end().addClass("test");
alert($('#first').html);//这是你之前的例子,你可以看看打印出来的是什么
$("#first div p").remove();//然后我们移除这前追加进来的p元素
var _p=$("<p>Hello world!</P>");//然后我们用另一种方法来达到效果
_p.appendTo($("#first div"));//一样的,追加进div内
_p.addClass("test");//给_p附加样式
alert($('#first').html);//再打印,跟之前的是一样的
</script>