您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

jquery怎么动态增加元素

2025/4/28 2:58:56发布11次查看
方法:1、“元素对象.append(增加元素)”在内部结尾增加;2、“元素对象.prepend(增加元素)”在内部开头增加;3、“元素对象.before(增加元素)”在元素的之前增加;4、“元素对象.after(增加元素)”在元素之后增加。
本教程操作环境:windows10系统、jquery3.2.1版本、dell g3电脑。
jquery怎么动态增加元素1、append
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。
语法为:
$(selector).append(content)
示例如下:
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").append(" <b>hello world!</b>"); });});</script></head><body><p>this is a paragraph.</p><p>this is another paragraph.</p><button>在每个 p 元素的结尾添加内容</button></body></html>
输出结果:
2、prepend
prepend() 方法在被选元素的开头(仍位于内部)插入指定内容。
语法为:
$(selector).prepend(content)
示例如下:
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").prepend("<b>hello world!</b> "); });});</script></head><body><p>this is a paragraph.</p><p>this is another paragraph.</p><button>在每个 p 元素的开头插入内容</button></body></html>
输出结果:
3、before
before() 方法在被选元素之前插入指定的内容。
语法为:
$(selector).before(content,function(index))
示例如下:
</script><script>$(document).ready(function(){$("button").click(function(){$("p").before("<p>hello world!</p>");});});</script></head><body><button>在p元素之前插入内容</button><p>这是一个段落。</p><p>这是另一个段落。</p></body>
输出结果:
4、after
after() 方法在被选元素后插入指定的内容。
语法为:
$(selector).after(content,function(index))
示例如下:
<script>$(document).ready(function(){ $("button").click(function(){ $("p").after("<p>hello world!</p>"); });});</script></head><body><button>在每个p元素后插入内容</button><p>这是一个段落。</p><p>这是另一个段落。</p></body>
输出结果:
相关视频教程推荐:jquery视频教程
以上就是jquery怎么动态增加元素的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product