admin管理员组文章数量:1389897
I am a jQuery started so if it is not of good quality forgive me.
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){
}
My Working code --
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example</title>
<script type="text/javascript" src=".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>
My tries --
$( 'li' ).html(function( 3, oldHtml ) {....
$( 'li' ).html(function( "3", oldHtml ) {....
$( 'li' ).eq(3).html(function( "3", oldHtml ) {......
I am a jQuery started so if it is not of good quality forgive me.
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){
}
My Working code --
<!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./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>
My tries --
$( 'li' ).html(function( 3, oldHtml ) {....
$( 'li' ).html(function( "3", oldHtml ) {....
$( 'li' ).eq(3).html(function( "3", oldHtml ) {......
Share
Improve this question
edited Jan 25, 2014 at 18:20
rene
42.5k78 gold badges121 silver badges165 bronze badges
asked Sep 17, 2012 at 9:37
TrialcoderTrialcoder
6,01411 gold badges46 silver badges66 bronze badges
5 Answers
Reset to default 5The 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;
});
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>
index means the number that points the position of a certain element selected by jquery..
for example the selected element is $('#haha')
<ul id='haha'>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
so the first li
is index 0, then 1 and so forth
you make use of index() when looking at data in terms of a collection. eg
<ul>
<li id="foo">foo</li>
<li id="bar">bar</li>
<li id="baz">baz</li>
</ul>
var listItem = document.getElementById('bar');
alert('Index: ' + $('li').index(listItem));
would give you the index of the bar item in the scope of the li list
see jquery documentation
Description: Search for a given element from among the matched elements.
Index is the order of the element, it will tell you which index this element has. The first one will have index 0 and so on, is very mon in many programming languages and jQuery has this tool for you.
jQuery creates an array of your returned elements, a collection of elements referenced by an index. If you return the typeof
it will logically return number as it's a number.
jsBin demo
$( 'li' ).html(function( index, html ) {
if(index===2){ // zero based, so it will colour the 3rd element!
$(this).css({color:'red'});
}
return 'jQuery indexes me inside an array as n:'+ index+
'<br> My HTML: '+ html + '!!!';
});
本文标签: javascriptWhat does index in jquery functions meansStack Overflow
版权声明:本文标题:javascript - What does index in jquery functions means - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744653375a2617820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论