gpt4 book ai didi

javascript - Uncaught ReferenceError : $ is not defined how to solve?

转载 作者:行者123 更新时间:2023-11-29 10:37:54 25 4
gpt4 key购买 nike

我在 cordova build 中点击按钮重定向到另一个页面时遇到问题,错误是 Uncaught ReferenceError: $ is not defined

// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkID=397704
// To debug code on page load in Ripple or on Android devices/emulators: launch your app, set breakpoints,
// and then run "window.location.reload()" in the JavaScript Console.
(function () {
"use strict";

document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

function onDeviceReady() {
// Handle the Cordova pause and resume events
document.addEventListener( 'pause', onPause.bind( this ), false );
document.addEventListener('resume', onResume.bind(this), false);
$("getas").click(function () {
alert("The paragraph was clicked.");
});


// TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
};

function onPause() {
// TODO: This application has been suspended. Save application state here.
};

function onResume() {
// TODO: This application has been reactivated. Restore application state here.
};
} )();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />

<!--
Customize the content security policy in the meta tag below as needed. Add 'unsafe-inline' to default-src to enable inline JavaScript.
For details, see http://go.microsoft.com/fwlink/?LinkID=617521
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<title>CatechesisMobileApp</title>

<!-- MobileApp references -->
<link href="css/index.css" rel="stylesheet" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="cordova.js"></script>
<script src="scripts/platformOverrides.js"></script>

<script src="scripts/index.js"></script>

</head>
<body>
<p>Mobile App - Inicio</p>

<button id="getas">Ver Alunos (GET ALL)</button>
<br />
<br />
<button id="geta">Ver Aluno (GET ID)</button>
<br />
<br />
<button id="posta">Inserir Aluno (POST)</button>

<!-- Cordova reference, this is added to your app when it's built. -->

</body>
</html>

我做错了什么?我已经尝试了 onclick 按钮“document.location.href='example.html'”,但根本不起作用

谢谢

最佳答案

Philip Clegg 的评论非常接近。您需要将 jQuery 传递到闭包中。这是一个 SO question explaining it

(function ($) {
"use strict";

document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

function onDeviceReady() {
// Handle the Cordova pause and resume events
document.addEventListener( 'pause', onPause.bind( this ), false );
document.addEventListener('resume', onResume.bind(this), false);
$("#getas").click(function () {
alert("The paragraph was clicked.");
});


// TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
};

function onPause() {
// TODO: This application has been suspended. Save application state here.
};

function onResume() {
// TODO: This application has been reactivated. Restore application state here.
};
} )(jQuery);

关于javascript - Uncaught ReferenceError : $ is not defined how to solve?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33902062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com