$("*",$(“#container1"))和$(“#container2").find("*")之间有什么区别? [英] What is the difference between $("*", $("#container1")) and $("#container2").find("*")?

查看:140
本文介绍了$("*",$(“#container1"))和$(“#container2").find("*")之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$("*", $("#container1"))$("#container2").find("*")有什么区别? 我通常使用AA,但在这种情况下效果不佳,可能会更加奇怪.

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()).getTime();

    $("#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

推荐答案

正确使用时,上下文选择器与find没什么不同. Resig表示他不喜欢上下文选择器,而是希望人们使用.find(),因为它在语义上更有意义.

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.

有很多方法可以弄乱上下文;例如,传递字符串不起作用,并使选择器默认解析整个文档.我可以肯定您的示例正确使用了上下文(没有时间进行测试),但是再次使用.find()克服了这种不确定性.

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.

这篇关于$("*",$(“#container1"))和$(“#container2").find("*")之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆