2

The following code gives me an alert with nothing but a # symbol inside it. Why?

EDIT: I should note that the code is inside a jQuery .click event...if I place it outside of that, it works properly. Here is the fuller code:

 $('#continue').click(function(){
                                 var _href = $("#continue").attr("href");
                                 alert(_href);                                
            });


<a href="selectRadius.html" data-icon="arrow-r" id="continue" style="float:right;" data-role="button" data-inline="true">Continue</a>

EDIT2: This all works fine in jsfiddle. But in the xcode iphone simulator I just get #.

4

3 に答える 3

3

入力したコードだけから判断すると、コードの実行が早すぎる可能性があります。JSをラップしてみてください

$(function() {
    // your code here
});

または

$(document).ready(function(){ 
    // your code here
});

アップデート:

まあ、これは iPhone シミュレーターなので、状況が変わります。問題の詳細をすべて説明しない限り、相手がどんなに経験を積んでいても、誰もあなたを助けることはできないということを忘れないでください。

クリックの代わりに touchstart / touchend / tap イベントを試しましたか? 私の知る限り、Apple はクリック イベントに問題を抱えています。また、モバイル デバイスのクリック イベントは応答が遅くなります (よく覚えている場合は約 300 ミリ秒の遅延)。

何を構築していますか?それはモバイルウェブアプリですか?標準のモバイル ブラウザや PhoneGap などで動作しますか?

更新 2:

Ok。コードが Click で呼び出されない限り機能します。これにより、コードがブロック内にある必要があるため、別のコードが「href」を別の値に置き換える可能性がなくなります$('#continue').click(function(){ });

クリック イベントはタッチ フォンでシミュレートされます。そのため、タッチ イベントはより高速で (ネイティブであり)、問題を引き起こす可能性が低くなります。また、そこで false を返し、リンクをたどらないようにする必要があります。これが「href」を置き換えている可能性があります。

于 2012-05-27T02:56:45.033 に答える
1
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){ 
    $('#continue').click(function(e) {
        var _href = $(this).attr('href');
        alert(_href);

        e.preventDefault();
        return(false);
        /*
           the return is legacy code, used by some
           browsers to detect if current event handler
           is braking default behaviour

           the e.preventDefault() function is the jQuery
           way to do it
        */
    });
});
</script>

<a href="selectRadius.html" data-icon="arrow-r" id="continue" style="float:right;" data-role="button" data-inline="true">Continue</a>

この行がないと、リンクがたどられ、更新が発生して現在のスクリプトが強制終了されます。

于 2012-05-27T03:15:53.980 に答える
0

https://github.com/jquery/jquery-mobile/issues/3777

于 2012-05-27T03:43:34.577 に答える