本文介绍了向标签中动态插入html分为向标签内部、外部插入,此外还有替换标签内部原来的html内容。
快捷导航,点击快速查看
①prepend();(标签内、文首插入)②append(); (标签内、文末插入)
③before();(标签外、标签首插入)
④after();(标签外、标签文末插入)
⑤html()(标签内部、html替换)
一、prepend()
语法:$().prepend(String/Element/JQuery);
插入位置:标签内部起始位置插入
效果展示:
拓展:此外prependTo()方法与其作用效果相同但使用不同,
需要调换 $(“p”).prepend(“Hello < b>world!< /b>”);语句中标签名和插入html的位置,
像这样 $(“Hello < b>world!< /b>”).prependTo(“p”);
二、append()
语法:$(selector).append(String/Element/JQuery);
插入位置:标签内部结尾位置插入
效果展示:
拓展:此外appendTo()方法与其作用效果相同,使用方法同prependTo()方法$(content).appendTo(selector);。
三、 before()
语法:$(selector).before(String/Element/JQuery);
插入位置:标签外部起始位置插入
效果展示:
拓展:insertBefore()–>待了解。
四、after()
语法:$(selector).after(String/Element/JQuery);
插入位置:标签外部起始位置插入
效果展示:
拓展:insertAfter()–>待了解。
五、html()
语法:$(selector).html(String/Element/JQuery);
插入位置:p标签内部,替换标签里的html内容
效果展示:
button点击前:
button点击后:
本文测试地址:https://www.w3school.com.cn/tiy/t.asp?f=jquery_manipulation_html_set
------------------------------------我是分割线------------------------------------------
没有一篇CSDN解决不了的问题,如果有,那就两篇