1

我正在使用 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>
4

1 回答 1

0

在 HTML 标记中,使用此代码。

<p id="ax"></p>
<p id="ay"></p>
<p id="az"></p>

以及 javascript 代码中的 onSuccess(acceleration) 函数。你应该使用这个命令

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);
于 2012-05-19T06:03:30.400 回答