jQuery 文档操作 - after() 方法 概述
jQuery 2022-06-01 15:23:2911小码哥的IT人生shichen
jQuery 文档操作 - after() 方法
实例
在每个 p 元素后插入内容:
$("button").click(function(){ $("p").after("<p>Hello world!</p>"); });
完整实例:
<html> <head> <script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").after("<p>Hello world!</p>"); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button>在每个 p 元素后插入内容</button> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
定义和用法
after() 方法在被选元素后插入指定的内容。
语法
$(selector).after(content)
参数 | 描述 |
---|---|
content | 必需。规定要插入的内容(可包含 HTML 标签)。 |
使用函数来插入内容
使用函数在被选元素之后插入指定的内容。
语法
$(selector).after(function(index))
完整实例:
<html> <head> <script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").after(function(n){ return "<p>The p element above has index " + n + "</p>"; }); }); }); </script> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>在每个 p 元素后插入内容</button> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
参数 | 描述 |
---|---|
function(index) |
必需。规定返回待插入内容的函数。
|