问题描述
我是一个jQuery,所以如果质量不高,请原谅我。
I am a jQuery started so if it is not of good quality forgive me.
我想知道索引是什么
表示函数中的含义以及它的确切含义。以前我认为它指的是像0,1,2,3等索引号,但当我通过1,2,3代替索引时,我的代码停止工作。我检查了它的类型,它显示我数字
数据类型。
现在让我告诉我究竟是什么错误以及jQuery中的索引和元素的概念,因为大多数地方我发现了这样的东西 -
I want to know what does index
means in the function and what exactly it refers too. Previously i thought it refers to the index number like 0,1,2,3 etc but when i passed 1,2 ,3 in place of index my code stops working. I checked the type of this and it is showing me number
data type.Let me now what exactly im doing wrong and the concept of index and Element in jQuery as most places i found something like this --
function(e){
}
我的工作代码 -
<!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>
我的尝试 -
$( 'li' ).html(function( 3, oldHtml ) {....
$( 'li' ).html(function( "3", oldHtml ) {....
$( 'li' ).eq(3).html(function( "3", oldHtml ) {......
推荐答案
index
参数代表匹配集合中元素的索引。你不应该向它传递值。它是一个传递给你可以在里面使用的匿名函数的参数,以确切知道在你需要的时候调用这个匿名函数的元素to:
The index
argument represents the index of the element in the matched collection. You should not be passing values to it. It is an argument that is passed to the anonymous function that you could use inside to know exactly on which element this anonymous function is being invoked if you needed to:
$( 'li' ).html(function( index, oldHtml ) {
return 'new html ' + index;
});
索引基于零,因此结果将是be:
The index is zero based, so the result will be:
<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>
这篇关于jquery函数中的索引意味着什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!