簡體   English   中英

無法提醒href val?

[英]Can't alert href val?

以下代碼給了我一個警報,里面只有#符號。 為什么?

編輯:我應該注意,代碼在jQuery .click事件中...如果我將其放置在該事件之外,它將正常工作。 這是完整的代碼:

 $('#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:這一切在jsfiddle中都可以正常工作。 但是在xcode iphone模擬器中,我只得到#。

僅從您鍵入的代碼來看,該代碼可能運行得太早。 嘗試將JS包裝在

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

要么

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

更新:

好吧,因為它是iPhone模擬器,所以它可以改變一切。 請記住,除非您提供問題的所有細節,否則無論他們有多少經驗,沒有人能為您提供幫助。

您是否嘗試了touchstart / touchend / tap事件而不是click? 據我所知,Apple在點擊事件方面一直存在問題。 此外,移動設備上的點擊事件的響應速度會較慢(如果我記得很好,則延遲約為300毫秒),因此最好使用特定於觸摸的事件。

你在建什么 它是移動網絡應用程序還是? 它可以在標准的移動瀏覽器或PhoneGap等工具中運行嗎?

UPDATE2:

好。 只要未在Click上調用該代碼,它就起作用。 這樣就消除了另一段代碼用另一個值替換“ href”的可能性,因為該代碼必須位於$('#continue').click(function(){ }); 塊。

單擊事件是在觸摸電話上模擬的,這就是為什么觸摸事件更快(它們是本機的)並且不太可能引起問題的原因。 您還應該確保在此處返回false,而不點擊鏈接,這可能是替換“ href”的原因。

<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>

如果沒有此行,將跟隨該鏈接,並且刷新將終止當前腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM