我正在使用phonegap获取加速度计的值并打印出来,但我不确定如何在主体中显示它。我也在使用JQuery。此代码取自Cordova(PhoneGap)网站上的加速计示例。
<!DOCTYPE html>
<html>
<head>
<title>Acceleration Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
}
// onSuccess: Get a snapshot of the current acceleration
//
function onSuccess(acceleration) {
alert('Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
document.writeln(
'Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n');
}
// onError: Failed to get the acceleration
//
function onError() {
alert('onError!');
}
</script>
</head>
<body>
<h1>Example</h1>
<p>getCurrentAcceleration</p>
</body>
</html>
答案 0 :(得分:0)
在HTML标记中,使用此代码。
<p id="ax"></p>
<p id="ay"></p>
<p id="az"></p>
和javascript代码中的onSuccess(加速)功能。你应该使用这个命令
document.getElementById("ax").innerHTML = acceleration.x;
document.getElementById("ay").innerHTML = acceleration.y;
document.getElementById("az").innerHTML = acceleration.z;
或使用像这样的jQuery
$("ax").html(acceleration.x);
$("ay").html(acceleration.y);
$("az").html(acceleration.z);