jQuery添加新内容的四个常用方法分析【append,prepend,after,before】
本文实例讲述了jQuery添加新内容的四个常用方法。分享给大家供大家参考,具体如下:
添加新内容的四个jQuery方法区别如下:
append()-在被选元素(里面)的结尾插入内容
prepend()-在被选元素(里面)的开头插入内容
//jQueryappend()方法在被选元素的结尾插入内容。 $("p").append("Someappendedtext.");
//jQueryprepend()方法在被选元素的开头插入内容。 $("p").prepend("Someprependedtext.");
通过append()和prepend()方法添加若干新元素
functionappendText() { vartxt1="Text.
";//以HTML创建新元素 vartxt2=$("").text("Text.");//以jQuery创建新元素 vartxt3=document.createElement("p");//以DOM创建新元素 txt3.innerHTML="Text."; $("p").append(txt1,txt2,txt3);//追加新元素 }
after()-在被选元素(外面)之后插入内容
before()-在被选元素(外面)之前插入内容
$("img").after("Sometextafter");
$("img").before("Sometextbefore");
通过after()和before()方法添加若干新元素
functionafterText() { vartxt1="I";//以HTML创建新元素 vartxt2=$("").text("love");//通过jQuery创建新元素 vartxt3=document.createElement("big");//通过DOM创建新元素 txt3.innerHTML="jQuery!"; $("img").after(txt1,txt2,txt3);//在img之后插入新元素 }
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery页面元素操作技巧汇总》、《jQuery常见事件用法与技巧总结》、《jQuery常用插件及用法总结》、《jQuery扩展技巧总结》及《jquery选择器用法总结》
希望本文所述对大家jQuery程序设计有所帮助。