Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jquery
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
jquery
Commits
3cfb134a
Commit
3cfb134a
authored
Aug 04, 2011
by
Dave Methvin
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #367 from gnarf37/index-optimize
Quick improvement to the performance of .index() with no arguments
parents
8d5c8742
9a96af1d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
7 deletions
+14
-7
traversing.js
src/traversing.js
+10
-5
traversing.js
test/unit/traversing.js
+4
-2
No files found.
src/traversing.js
View file @
3cfb134a
...
@@ -145,12 +145,17 @@ jQuery.fn.extend({
...
@@ -145,12 +145,17 @@ jQuery.fn.extend({
// Determine the position of an element within
// Determine the position of an element within
// the matched set of elements
// the matched set of elements
index
:
function
(
elem
)
{
index
:
function
(
elem
)
{
if
(
!
elem
||
typeof
elem
===
"string"
)
{
return
jQuery
.
inArray
(
this
[
0
],
// No argument, return index in parent
// If it receives a string, the selector is used
if
(
!
elem
)
{
// If it receives nothing, the siblings are used
return
(
this
[
0
]
&&
this
[
0
].
parentNode
)
?
this
.
prevAll
().
length
:
-
1
;
elem
?
jQuery
(
elem
)
:
this
.
parent
().
children
()
);
}
// index in selector
if
(
typeof
elem
===
"string"
)
{
return
jQuery
.
inArray
(
this
[
0
],
jQuery
(
elem
)
);
}
}
// Locate the position of the desired element
// Locate the position of the desired element
return
jQuery
.
inArray
(
return
jQuery
.
inArray
(
// If it receives a jQuery object, the first element is used
// If it receives a jQuery object, the first element is used
...
...
test/unit/traversing.js
View file @
3cfb134a
...
@@ -99,9 +99,11 @@ test("is(jQuery)", function() {
...
@@ -99,9 +99,11 @@ test("is(jQuery)", function() {
});
});
test
(
"index()"
,
function
()
{
test
(
"index()"
,
function
()
{
expect
(
1
);
expect
(
2
);
equal
(
jQuery
(
"#text2"
).
index
(),
2
,
"Returns the index of a child amongst its siblings"
);
equal
s
(
jQuery
(
"#text2"
).
index
(),
2
,
"Returns the index of a child amongst its siblings"
)
equal
(
jQuery
(
"<div/>"
).
index
(),
-
1
,
"Node without parent returns -1"
);
});
});
test
(
"index(Object|String|undefined)"
,
function
()
{
test
(
"index(Object|String|undefined)"
,
function
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment