---
title: "Jquery学习appendTo和after区别和应用"
author: "东流水"
date: "2011-02-23T15:50:00Z"
tags: []
categories: ["Web前端"]
slug: "jquery-appendto-after"
draft: false
lastmod: "2011-08-25T01:10:05Z"
id: 919712
url: "https://992012.iteye.com/blog/919712"
---

appendTo(content)解释为：

把所有匹配的元素追加到另一个指定的元素元素集合中。实际上，使用这个方法是颠倒了常规的$(A).append(B)的操作，即不是把B追加到A中，而是把A追加到B中。

 

Api上的例子:

```
HTML 代码:

<p>I would like to say: </p>
<div></div><div></div>
jQuery 代码:

$("p").appendTo("div");
结果:

<div><p>I would like to say: </p></div>
<div><p>I would like to say: </p></div>
```

实际应用：

   &lt;s:iterator value="classGradeList"&gt;  
    $("&lt;option&gt;"+"&lt;s:property value='gradeName' /&gt;"+"&lt;/option&gt;").appendTo("#gradeId").val("&lt;s:property value='gradeId' /&gt;");  
         &lt;/s:iterator&gt;

上面是用到了struts2，classGradeList为后台返回的一个list集合，里面含有两个属性'gradeName' ，和gradeId。得到结果则为后台返回的所有学年度的下拉框。

 

 

after(content)解释

在每个匹配的元素之后插入内容。

API例子:

##### HTML 代码:

```
<p>I would like to say: </p>
```

##### jQuery 代码:

```
$("p").after("<b>Hello</b>");
```

##### 结果:

```
<p>I would like to say: </p><b>Hello</b>
```

 

实际应用：

$.ajax({  
   [url:"/school\_class/studentInfoAction\_validatorCode.action?studentInfoVo.studentNo="+$("#studentNo").val()+"&studentInfoVo.studentinfoId="+$("#id").val](/admin/blogs/%22/school_class/studentInfoAction_validatorCode.action?studentInfoVo.studentNo=%22%20%24%28%22#studentNo%22%29.val%28%29+%22&studentInfoVo.studentinfoId=%22+$%28%22%23id%22%29.val)(),  
   type:"post",  
   async:false,  
   timeout:3000,  
   success:function(html) {  
    resultMess=html;  
    if(html==""){  
         $("#studentNoMess").after("&lt;span id='loadmsg' style='color: green'&gt;可以使用&lt;/span&gt;");  
        $("#allow").show();  
    }else{  
        $("#studentNo").focus();  
    }  
   }   
  });

页面中有个div 它的Id为studentNoMess，功能是检查学号是否有重复的例子。