我的$(文件).ready内部没有任何内容

gjw*_*w80 4 javascript jquery express pug

我整天都在环顾四周,无法弄清楚为什么我的$(document).ready包装器中的代码都不起作用.

layout.jade

!!!5
html
    head
        title #{title}
        link(rel='stylesheet', href='/stylesheets/style.css')
        script(src='http://code.jquery.com/jquery-1.js')
        script.
            $(document).ready(function(){
                alert("working");
                $("#message").html("message set through jquery");
            });
    body
        header
            h1 Sample message here
        .container
            .main-content
                block content
            .sidebar
                block sidebar
        footer
            block foot
Run Code Online (Sandbox Code Playgroud)

landing.jade

extends layout
block content
    p#message Landing page
    #info Info area
block foot
    a(href="https://localhost:8888/logout", title="logout") Logout
Run Code Online (Sandbox Code Playgroud)

控制器:

exports.landing = function(req, res) {
    res.render('landing', {title: 'My Title'});
}
Run Code Online (Sandbox Code Playgroud)

渲染的html:

<!DOCTYPE html><html><head><title>Dashboard</title><link rel="stylesheet" href="/stylesheets/style.css"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){
    $("#message").html("message set through jquery");
});
alert("working");</script></head><body><header><h1>Sample Web App</h1></header><div class="container"><div class="main-content"><p id="message">Landing page</p><div id="info">Info area</div></div><div class="sidebar"></div></div><footer><a href="https://localhost:8888/logout" title="logout">Logout</a></footer></body></html>
Run Code Online (Sandbox Code Playgroud)

控制台错误:我刚刚检查了页面上的控制台,而不是我正在运行我的Express Web服务器,并发现错误:

Uncaught reference error: $ is not defined
Run Code Online (Sandbox Code Playgroud)

https服务器问题:

主要问题有两个:1)我正在使用@Joe认可的错误网址.2)由于我的Web服务器是在Express中以https而不是http创建的,因此它拒绝使用@ Joe的答案中列出的非安全http地址的URL.相反,我需要将其修改为https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js,以便我的https网络服务器将按照@FrédéricHamidi的建议使用它.

Fré*_*idi 7

你的开发环境看起来很奇怪:)

jQuery脚本无法加载,因为它是通过HTTP提供的,主文档是通过HTTPS提供的,并且您的浏览器都已配置为静默删除通过HTTPS提供的文档生成的HTTP请求.

幸运的是,Google CDN支持HTTP和HTTPS,因此您只需在脚本源URL中切换协议:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
Run Code Online (Sandbox Code Playgroud)

  • @Alder,呵呵:)实际上,协议是关键所在,所以你的答案不会起作用,因为它也使用HTTP而不是HTTPS来获取脚本.另一方面,dc2(通过Joe)的答案可能会起作用,因为它没有指定协议(普通`// ajax.googleapis.com /`),在这种情况下,它将默认为使用的协议主要文件IIRC. (2认同)