未捕获的ReferenceError: MyFunction未定义
Uncaught ReferenceError: MyFunction is not defined - PhoneGap
我使用PhoneGap和jQuery Mobile移动web应用程序。当我尝试运行MyFunction设置在onclick属性上时,我在控制台得到一个错误,
Uncaught ReferenceError: MyFunction is not defined.
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Product</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" href="css/jquery.mobile-1.3.1.min.css" />
<script type="text/javascript" src="cordova-2.7.0.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.3.1.min.js"></script>
<!--Generation Script Starts here -->
<script type="text/javascript">
function makeid()
{
var text = "";
var possible = "123456789";
for( var i=0; i < 4; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function makeid1()
{
var text1 = "";
var possible = "123456789";
for( var i=0; i < 1; i++ )
text1 += possible.charAt(Math.floor(Math.random() * possible.length));
return text1;
}
function makeid2()
{
var text2 = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for( var i=0; i < 1; i++ )
text2 += possible.charAt(Math.floor(Math.random() * possible.length));
return text2;
}
function makeid3()
{
var text3 = "";
var possible = "123456789";
for( var i=0; i < 2; i++ )
text3 += possible.charAt(Math.floor(Math.random() * possible.length));
return text3;
}
function myFunction()
{
document.getElementById("demo").innerHTML="D01E-BOA0-" + makeid() + "-" + makeid1() + makeid2() + makeid3();
}
</script>
<!-- Generation Script Ends Here -->
</head>
<body>
<div data-role="page">
<div data-role="header" data-theme="b">
<a href="index.html" data-role="button" data-icon="home" data-iconpos="notext" data-theme="b" data-iconshadow="false" data-inline="true">Home</a>
<h1>Product</h1>
</div><!-- /header -->
<div data-role="content">
<p id="demo" style="text-align:center;"></p>
<input type="button" onclick="myFunction()" value="Generate!" data-theme="b">
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
我不确定这是否有帮助,将所有代码打包成文档准备,并且使用jQuery处理click
按钮:
<input type="button" id="generate" value="Generate!" data-theme="b">
$('#generate').on('click', function () {
$("#demo").html("D01E-BOA0-" + makeid() + "-" + makeid1() + makeid2() + makeid3());
});
完整代码:
$(function () {
function makeid() {
var text = "";
var possible = "123456789";
for( var i=0; i < 4; i++ ) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function makeid1() {
var text1 = "";
var possible = "123456789";
for( var i=0; i < 1; i++ ) {
text1 += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text1;
}
function makeid2() {
var text2 = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for( var i=0; i < 1; i++ ) {
text2 += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text2;
}
function makeid3() {
var text3 = "";
var possible = "123456789";
for( var i=0; i < 2; i++ ) {
text3 += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text3;
}
$('#generate').on('click', function () {
$("#demo").html("D01E-BOA0-" + makeid() + "-" + makeid1() + makeid2() + makeid3());
});
});
这可能会解决您的问题:
-
为onLoad添加一个监听器:
<body onLoad="fireItUp()">
-
使用被调用的方法为cordova的deviceready添加一个事件监听器:
function fireItUp() { document.addEventListener("deviceready", onDeviceReady, false); }
-
使用回调方法将您的事件绑定到jquery mobile 'tap'事件(您可能想要给按钮一个id或类,因为这会绑定到所有输入元素,这可能是不希望的):
function onDeviceReady() { jQuery(input).bind('tap',function(event){ myFunction(); }); }
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 节点fs.stat名称未定义
- 引用错误:未定义 myfunction
- 未捕获的语法错误:意外的标记<和未捕获的引用错误:未定义 myFunction
- 未捕获引用-myFunction()未定义
- 未捕获的ReferenceError:myFunction未在null处定义:webview中的Android异常1
- Am在chrome调试器中加载时收到错误myFunction未定义
- 未捕获的ReferenceError: MyFunction未定义
- 未捕获的ReferenceError: myFunction未定义