为了账号安全,请及时绑定邮箱和手机立即绑定

麻烦帮忙看一下关于jquery中append函数的问题~~谢谢

麻烦帮忙看一下关于jquery中append函数的问题~~谢谢

慕标琳琳 2022-01-13 20:11:04
有如下代码:<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){$("button").click(function(){$("p").append(function(m){return "<b>This p element has index " + m + "</b>";});});});</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>点击一次按钮后的结果是:This is a headingThis is a paragraph.This p element has index 0This is another paragraph.This p element has index 1问:那个参数m是如何根据不同的匹配元素自增的呢?PS:如果我给$("p").append(function(m)写两个参数,即$("p").append(function(m,n),下边也相应的改成return "<b>This p element has index " + (m+n) + "</b>";会怎么样呢?求达人讲解原理~~~结果中的0和1是怎么来的呢?
查看完整描述

2 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

append:向每个匹配的元素内部追加内容。
举个例子:
html代码:<p>I would like to say: </p>
jquery 代码: $("p").append("<b>Hello</b>");
结果:<p>I would like to say: <b>Hello</b></p>

查看完整回答
反对 回复 2022-01-17
?
慕容708150

TA贡献1831条经验 获得超4个赞

添加内容用比如$("div").append("<div></div>")
这样就是给div添加一个子节点div

查看完整回答
反对 回复 2022-01-17
  • 2 回答
  • 0 关注
  • 226 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信