jQuery函数中的索引意味着什么

Tri*_*der 4 javascript jquery logic

我是从jQuery开始的,所以如果质量不好,请原谅我。

我想知道index函数中的含义以及它所指的确切含义。以前我以为它引用的索引号是0、1、2、3等,但是当我通过1,2,3代替索引时,我的代码停止工作。我检查了它的类型,它显示了我的number数据类型。现在让我看看我到底在做什么错以及jQuery中index和Element的概念,因为我在大多数地方都发现了这样的东西-

function(e){
}
Run Code Online (Sandbox Code Playgroud)

我的工作代码-

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
    <title>Example</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$( 'li' ).html(function( index, oldHtml ) {
//alert(typeof($(this).index()));
  return oldHtml + '!!!'
});
});
</script>
</head>
<body>

<ul>
<li>This is List item 1</li>
<li>This is List item 2</li>
<li>This is List item 3</li>
<li>This is List item 4</li>
<li>This is List item 5</li>
</ul>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的尝试-

$( 'li' ).html(function( 3, oldHtml ) {....

$( 'li' ).html(function( "3", oldHtml ) {....

$( 'li' ).eq(3).html(function( "3", oldHtml ) {......
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 5

index参数表示匹配的集合中的元素的索引。您不应将值传递给它。它是传递给匿名函数的参数,您可以在内部使用该参数确切知道在以下情况下将在哪个元素上调用此匿名函数:

$( 'li' ).html(function( index, oldHtml ) {
    return 'new html ' + index;
});
Run Code Online (Sandbox Code Playgroud)

索引是从零开始的,因此结果将是:

<li>new html 0</li>
<li>new html 1</li>
<li>new html 2</li>
<li>new html 3</li>
<li>new html 4</li>
Run Code Online (Sandbox Code Playgroud)