Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery mobile Jquery移动性能?_Jquery Mobile_Jquery_Event Bubbling - Fatal编程技术网

Jquery mobile Jquery移动性能?

Jquery mobile Jquery移动性能?,jquery-mobile,jquery,event-bubbling,Jquery Mobile,Jquery,Event Bubbling,请在下面找到2组代码块,让我知道要遵循哪一个以及为什么 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Set 1</title> <link rel="sty

请在下面找到2组代码块,让我知道要遵循哪一个以及为什么

<!DOCTYPE html> 
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Set 1</title> 
    <link rel="stylesheet" href="css/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="css/customStyle.css" />    
</head> 

<body> 

<div data-role="page">      
    <div data-role="header" data-position="fixed"></div>
    <div data-role="content"></div>
    <div data-role="footer" data-position="fixed"></div>

</div>

    <script src="js/jquery-1.8.2.min.js"></script>
    <script src="js/customScript.js"></script>  
    <script src="js/jquery.mobile-1.2.0.js"></script>   

</body>
</html>

第1组
第二组是

<!DOCTYPE html> 
    <html>

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <title>Set 1</title> 
        <link rel="stylesheet" href="css/jquery.mobile-1.2.0.min.css" />
        <link rel="stylesheet" href="css/customStyle.css" />        

        <script src="js/jquery-1.8.2.min.js"></script>
        <script src="js/customScript.js"></script>  
        <script src="js/jquery.mobile-1.2.0.js"></script>
    </head> 

第1组

(即)将所有脚本放在顶部还是底部,哪一个是正确的?

第一种情况是提高性能。。。。据说这是一种将javascript保留在页面中的常规做法…以便更快地加载内容。。。。虽然我个人没有任何事实或分析。查看sameelegantcode.com/2010/03/30/这篇漂亮的文章,你的javascript去哪里了/

它们都是正确的,唯一的区别是如何绑定事件

在第一种情况下,因为HTML已经加载到DOM中,所以可以像这样直接绑定事件:

$('#buttonID').on('click', function(){       

});
因为按钮已经进入DOM,所以可以直接将click事件绑定到它

在第二种情况下,由于jQuery Mobile是在页面内容之前加载的,因此所有事件绑定都必须像委托一样完成:

$(document).on('click', '#buttonID',function(){       

});
这是一个更安全但速度较慢的解决方案。将事件绑定到对象不需要对象存在


简而言之,解决方案1的速度稍快一些

最好将所有javascript放在页面底部。永远不要忘记,javascript解析/执行可能会延迟页面呈现。嗯,我相信你喜欢阅读

谢谢你的回复!。。。所以,我们需要把所有的脚本放在标题部分只是为了提高性能-对吗?第一种情况…非常抱歉出现了严重的错误…是的,请检查提供的链接…保持js关闭。。。在放置位置上没有这样的限制。它应该降低以提高性能