<code><html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").append(function(n){
return "<b>This p element has index " + n + "</b>";
});
});
});
</script>
</head>
<body>
<h1>This is a heading</h1>
<p>this is a paragraph.</p>
<p>This is another paragraph.</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/c1c2c2ed740f" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Java免费学习笔记(深入)</a>”;</p>
<button>在每个 p 元素的结尾添加内容</button>
</body>
</html></code><code>$("p").append(function(n){
return "<b>This p element has index " + n + "</b>";</code>请问上面一段代码中的n代表什么意思,是直接定义一个变量还是什么的
<code><html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").append(function(n){
return "<b>This p element has index " + n + "</b>";
});
});
});
</script>
</head>
<body>
<h1>This is a heading</h1>
<p>this is a paragraph.</p>
<p>This is another paragraph.</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/c1c2c2ed740f" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Java免费学习笔记(深入)</a>”;</p>
<button>在每个 p 元素的结尾添加内容</button>
</body>
</html></code><code>$("p").append(function(n){
return "<b>This p element has index " + n + "</b>";</code>请问上面一段代码中的n代表什么意思,是直接定义一个变量还是什么的
<code>$.append(function(n){
});</code>这个方法的n你可以理解为index,从0开始的,详见jqueryApi Append
请参考: jQuery 中 append方法的说明
我来解答一下这个问题,这个是关于回调函数的问题,回调函数的具体用法请参考这里:链接描述,里面有关于我对于回调函数的回答
首先append是一个方法,这个方法属于$('p')对象,然后你$('p').append(传入一个参数);
你在这里给append传入一个函数作为append使用的参数,然后append这个方法使用你这个回调函数,然后这个值是append这个方法传入给这个回调函数使用的。
n是函数参数,是jQuery库调你的回调函数时传过来的。
append方法里传了一个匿名函数 ,n只是一个局部变量
n是append方法定义时的callback里可接收到参数。
$("p").append(function(n){
<code> return "<b>This p element has index " + n + "</b>"; 这里选择标签P,append就是在字符串后面增加的意思,里面的function返回的是<b>This p element has index " + n + "</b>,而这个返回就是当做了append()方法里的参数,也就是在“p”标签后面加上<b>This p element has index " + n + "</b> 如<p>this is a paragraph.</p>这句就变成了<p>this is a paragraph.<b>This p element has index " + n + "</b></p>当然这个n也是一个传入的参数,具体看函数的给定</code>
n这个参数写做index,或者会让你更加的清晰,明白,其实这个n指的是在匹配集合里的位置(索引位置),其实在官网文档里查一下就可以了
首先那是一个回调函数,其次这个n是回调函数的一个参数。
n不是变量而是函数参数,属于这个函数对象的一种特殊变量
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号