1

私が使用しているコードは、モバイルアプリ用です。モバイル画面で現在選択されているオブジェクトの位置を調べたい>ここでは、位置を見つけようとしている2つの段落要素ID「#first」と「#second」がありますが、常にHTML coords (0,0) 結果に返されます。これは、jquery モバイル関数の代わりに jquery 関数 .postion() を使用しているためだと思います。助けてください

<html>
  <head>
    <meta name="viewport" content="width=320; user-scalable=no" />

    <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title">

    <script type="text/javascript" charset="utf-8" src="main.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.2/jquery.mobile-1.1.0-rc.2.min.css" />
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.1.0-rc.2/jquery.mobile-1.1.0-rc.2.min.js"></script>

    <script>
     $("*", document.body).click(function (e) {
     var offset = $(this).position();
     e.stopPropagation();
     $("#result").text(this.tagName + " coords ( " + offset.left + ", " +
                                  offset.top + " )");
     });

    </script>

</head> 
<body> 

<div data-role="page">

    <div data-role="header">
        <h1>My Title</h1>
    </div><!-- /header -->
     <div data-role="content" id="objects">
        <p id="first">1st object</p>
        <p id="second">2nd object</p>
        <p id="result"></p>
    </div>


</div><!-- /page -->

</body>
</html>
4

1 に答える 1

3

これを試しましたか:

$(function () {
    $('#first,#second').click(function () {
       $("#result").text(this.tagName + " coords ( " + $(this).position().left + ", " + $(this).position().top + " )");
    });        
});​

デモ: http://jsfiddle.net/CKG9U/

于 2012-04-06T07:08:53.810 に答える