相关疑难解决方法(0)

为什么脚本在body标签的末尾

我知道这个问题被多次询问过,但我没有找到答案.那么为什么建议在body标签的末尾包含脚本以便更好地渲染?

来自Udacity课程https://www.udacity.com/course/ud884 - 在DOM和CSSOM准备就绪后开始渲染.JS是HTML解析阻塞,任何脚本在CSSOM准备好后启动.

所以,如果我们得到:

<html>
    <head>
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <title>CRP</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <!-- content -->
        <script src="script.js"></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

CRP将是:

CSSOM ready > JS execute > DOM ready > Rendering
Run Code Online (Sandbox Code Playgroud)

如果脚本在头部:

<html>
    <head>
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <title>CRP</title>
        <link rel="stylesheet" href="styles.css">
        <script src="script.js"></script>
    </head>
    <body>
        <!-- content -->
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

CRP将是相同的:

CSSOM ready > JS execute > DOM ready > Rendering
Run Code Online (Sandbox Code Playgroud)

这个问题只是关于"同步"脚本(没有async/defer属性).

html javascript css pageload web

16
推荐指数
3
解决办法
2万
查看次数

在Angular.js中使用<script async>标记

Ilya Grigorik建议<script async>尽可能使用标签.

有没有使用标签加载Angular.js应用程序的干净,首选方法,而不使用require.js或angular-seed推荐的$ script.js工具?

显而易见的问题是执行顺序.例如,阻止:

Uncaught ReferenceError: angular is not defined
Run Code Online (Sandbox Code Playgroud)

https://www.igvita.com/2014/05/20/script-injected-async-scripts-considered-harmful/

javascript performance asynchronous angularjs

13
推荐指数
1
解决办法
6875
查看次数

Javascript中的经典脚本v/s模块脚本

当我看到这个声明时,我正在浏览标签的WHATWG规范asyncdefer属性<script>:

经典脚本可以指定deferasync; 模块脚本可以指定async.

我经历了经典模块脚本的WHATWG定义,但并没有真正清晰.有人可以向我解释我是5,Javascript中经典模块脚本之间的区别吗?

javascript module es6-modules

12
推荐指数
2
解决办法
4640
查看次数

在jQuery中设置init函数

这里只是一个简单的问题,我想知道如何将它们设置为init()函数,然后在document.ready上运行该函数.此代码正在单独的main.js文件中使用.是否需要从索引页面调用?

$('#main_right_line_one').click(function(){
    $('#main_regular_layover, #main_deep_layover').fadeOut('slow', function(){
        $('#main_light_layover').fadeIn('slow');
    });
});

$('#main_right_line_two').click(function(){    
    $('#main_light_layover, #main_deep_layover').fadeOut('slow', function(){
        $('#main_regular_layover').fadeIn('slow');
    });
});

$('#main_right_line_three').click(function(){
    $('#main_light_layover, #main_regular_layover').fadeOut('slow', function(){
        $('#main_deep_layover').fadeIn('slow');
    });
});
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.我真的试图绕过这个,但我似乎找不到任何好的教程来解释init()足够我的特定代码.

jquery

11
推荐指数
2
解决办法
7万
查看次数

HTML5:为什么脚本标记需要放在body标记的末尾而不是body标记的开头?

在编写html时,一些网站建议在BODY标记的末尾编写脚本,而不是在BODY标记的开头写入,我读到这将增加页面刷新的性能,但是这有助于在html页面呈现时只有在DOM解析了所有标签之后?

javascript html5

8
推荐指数
2
解决办法
511
查看次数

在HTML文档中插入JavaScript的最佳位置

可能重复:
将<script>标记放在HTML标记中的最佳位置在哪里?

我应该在哪里放置javascript代码?

  <-- Here?
</body>
  <-- Here?
</html>
  <-- Here?
Run Code Online (Sandbox Code Playgroud)

我一直用上面</body>只是想知道这是"最好的"

html javascript

7
推荐指数
1
解决办法
1万
查看次数

同一页面上的jQuery和javascript代码不起作用

我有关于jQuery和javascript代码的问题; 当我在下面</head>和之间写下这个jQuery时<body>

<script type="text/javascript">
    var $j = jQuery.noConflict();
    $j(document).ready(function(){
        $j('#page_effect').fadeIn(3000);
    });
</script>
Run Code Online (Sandbox Code Playgroud)

然后在body标签中编写javascript代码

<script src="bubbles.js"></script>
<script type="text/javascript">
    bubblesMain(new Object({
        type : 'linear',
        minSpeed : 100,
        maxSpeed : 400,
        minSize : 30,
        maxSize : 55,
        num : 100,
        colors : new Array('#FF0000','#FFFFFF','#FFCC99', '#FF33CC')
    }));
</script>
Run Code Online (Sandbox Code Playgroud)

然后jQuery代码可以工作,但JavaScript代码不起作用.最后我发现当我在第一次加载后调整浏览器大小时,可以运行.

bubble.js是自动创建一个canvas元素,然后在canvas中使用动画引发一些气泡.

部分代码如下:

function bubblesMain(obj){
    bubbleResize();
    bubbles = new bubbleObject(obj);
    bubbles.createBubbles();
    setInterval(start,1000/60);
};

//WHEN WINDOW HEIGHT IS CHANGED, REMAKE THE CANVAS ELEMENT
window.onresize = function(event) {
    bubbleResize();
}

function bubbleResize(){
    var height = …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

6
推荐指数
1
解决办法
1667
查看次数

HTML5脚本标记确实推迟等待任何以前的异步脚本

HTML5脚本标记加载指令似乎很酷/sf/answers/1684926141/

是否可以异步加载一堆脚本,但有一个脚本等待执行基于异步完成.

<script src="//some.cdn/jquery.js" async></script>
<script src="//some.cdn/underscore.js" async></script>
<script src="/my-app-which-uses-_-and-jquery.js" defer></script>
Run Code Online (Sandbox Code Playgroud)

我的应用程序脚本是否可以保证在我的库之后执行,还是只能按顺序执行其他延迟脚本?

javascript html5

6
推荐指数
1
解决办法
879
查看次数

错误:[$ injector:unpr]未知提供者:$ stateProvider

我试图使用ui.router时遇到错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module bApp due to:
Error: [$injector:unpr] Unknown provider: $stateProvider
http://errors.angularjs.org/1.4.5/$injector/unpr?p0=%24stateProvider
at REGEX_STRING_REGEXP (http://localhost:9000/bower_components/angular/angular.js:68:12)
at http://localhost:9000/bower_components/angular/angular.js:4284:19
at getService (http://localhost:9000/bower_components/angular/angular.js:4432:39)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4464:13)
at runInvokeQueue (http://localhost:9000/bower_components/angular/angular.js:4379:35)
at http://localhost:9000/bower_components/angular/angular.js:4388:11
at forEach (http://localhost:9000/bower_components/angular/angular.js:336:20)
at loadModules (http://localhost:9000/bower_components/angular/angular.js:4369:5)
at createInjector (http://localhost:9000/bower_components/angular/angular.js:4294:11)
at doBootstrap (http://localhost:9000/bower_components/angular/angular.js:1655:20)
http://errors.angularjs.org/1.4.5/$injector/modulerr?p0=bApp&p1=Error%3A%20…F%2Flocalhost%3A9000%2Fbower_components%2Fangular%2Fangular.js%3A1655%3A20)
Run Code Online (Sandbox Code Playgroud)

angular.module('bApp', ['ui.router']);
angular.module('bApp').config(function($stateProvider, $urlRouterProvider) {
    urlRouterProvider.otherwise('/');

    $stateProvider
        .state('main', {
            url: "/",
            templateUrl: "/views/main.html"
        })

    .state('register', {
        url: "/register",
        templateUrl: "/views/register.html"
       
    });
});
Run Code Online (Sandbox Code Playgroud)
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-ui-router

6
推荐指数
2
解决办法
8896
查看次数

Bootstrap4下拉列表仅在第二次单击时有效

嘿伙计们,我正在研究一个正在使用bootstrap4的django项目,而且我在使用dropdown toggler时遇到了一些问题......

切换器仅在第二个链接后切换下拉菜单!

我做错了什么?

这是我的下拉HTML代码:

            <li class="nav-item dropdown show">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Dropdown Toggler
                </a>
                <div class="dropdown-menu" role="menu" aria-labelledby="navbarDropdown">
                 <a class="dropdown-item">Link 1</a>
                 <a class="dropdown-item">Link 2</a>
                </div>
            </li>
Run Code Online (Sandbox Code Playgroud)

drop-down-menu bootstrap-4

6
推荐指数
2
解决办法
6183
查看次数