What is the difference between $("*", $("#container1")) and $("#container2").find("*")?
What is the difference between $("*", $("#container1"))
and $("#container2").find("*")
?.
I usually use AA, but not well in that case can be more ultil the odd.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script language="JavaScript">
$(function(){
var endTime = 0, iniTime = 0, counter = 0;
iniTime = (new Date()).getTime();
$("*", $("#container1")).each(function()
{
counter++;
});
endTime = (new Date()).ge开发者_JAVA百科tTime();
$("#result").append("<div>Container enviroment -> "+counter+" "+(endTime-iniTime)+"</div>");
endTime = 0; iniTime = 0; counter = 0;
iniTime = (new Date()).getTime();
$("#container2").find("*").each(function()
{
counter++;
});
endTime = (new Date()).getTime();
$("#result").append("<div>Find method -> "+counter+" "+(endTime-iniTime)+"</div>");
});
</script>
</head>
<body>
<div id="result"></div>
<div id="container1">
<span></span>...
</div>
<div id="container2">
<span></span>...
</div>
</body>
</html>
Result:
IE8
Container enviroment -> 9752 282
Find method -> 9752 296
Chrome 4.0
Container enviroment -> 9752 65
Find method -> 9752 66
Firefox
Container enviroment -> 9752 135
Find method -> 9752 125
Safari
Container enviroment -> 9752 46
Find method -> 9752 51
When used properly, the context selector is no different from find. Resig has stated that he dislikes the context selector and would prefer people use .find(), since it makes more sense semantically.
There's a lot of ways to screw up context; for example, passing a string does not work and causes the selector to default to parsing the entire document. I'm fairly sure your example is using the context correctly (no time to test), but again, using .find() beats this uncertainty.
I believe the context selector must implicitly call find() itself.
Best to just use find() for simplicity.
EDIT: source code from 1.3.2:
// HANDLE: $(expr, [context])
// (which is just equivalent to: $(content).find(expr)
精彩评论