【问题标题】:Nothing inside my $(document).ready works我的 $(document).ready 里面什么都没有
【发布时间】:2013-10-17 07:20:38
【问题描述】:

我整天都在四处寻找,无法弄清楚为什么我的 $(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

着陆.翡翠

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

控制器:

exports.landing = function(req, res) {
    res.render('landing', {title: 'My Title'});
}

渲染的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>

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

Uncaught reference error: $ is not defined

https 服务器问题:

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

【问题讨论】:

  • 渲染出来的 HTML 是什么样子的?
  • "code.jquery.com/jquery-1.js" 正在抛出 404 - 你可能打算使用这个:"//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ?
  • 能否请我们看一下 html 输出的粘贴箱?控制台在说什么?谢谢
  • 您的控制台有错误吗?
  • @gjw80,再想一想,尝试使用https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js 作为 jQuery 脚本的源 URL。当通过 HTTPS 提供主文档时,您的浏览器可能被配置为静默拒绝加载 HTTP 资源。

标签: javascript jquery express pug


【解决方案1】:

您的开发环境似乎很奇特:)

jQuery 脚本无法加载,因为它是通过 HTTP 提供的,主文档是通过 HTTPS 提供的,并且您的两个浏览器都已配置为静默丢弃从通过 HTTPS 提供的文档发出的 HTTP 请求。

好在谷歌CDN同时支持HTTP和HTTPS,所以你只需要在脚本源URL中切换协议即可:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

【讨论】:

  • 哦,那是你在吵架,好吧,抱歉=)
  • @Alder,呵呵 :) 实际上,协议是这里的关键,所以你的答案也不起作用,因为它也使用 HTTP 而不是 HTTPS 来获取脚本。另一方面,dc2(通过 Joe)的回答可能会起作用,因为它没有指定协议(纯 //ajax.googleapis.com/),在这种情况下,它将默认使用主文档 IIRC 使用的协议。
【解决方案2】:

查看乔的评论:

"code.jquery.com/jquery-1.js";正在抛出 404 - 你可能是说 使用这个: “//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js”? – 乔 38 分钟前

这可能是你的答案。 +1 给乔。

【讨论】:

  • @gjw80,它应该可以工作:jsfiddle.net/7V8jy 我简单地通过了你的“渲染的 html”
【解决方案3】:

按照他们所说的更改链接。我改了https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js

【讨论】:

    猜你喜欢
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    相关资源
    最近更新 更多