简体   繁体   English

无法提醒href val?

[英]Can't alert href val?

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. 编辑:我应该注意,代码在jQuery .click事件中...如果我将其放置在该事件之外,它将正常工作。 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. EDIT2:这一切在jsfiddle中都可以正常工作。 But in the xcode iphone simulator I just get #. 但是在xcode iphone模拟器中,我只得到#。

Judging by only the code you typed, probably the code runs too early. 仅从您键入的代码来看,该代码可能运行得太早。 Try wrapping the JS in 尝试将JS包装在

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

Or 要么

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

Update: 更新:

Well, since it's an iPhone simulator, it changes things. 好吧,因为它是iPhone模拟器,所以它可以改变一切。 Remember, nobody can help you unless you give all the details of the problem, no matter how much experience they have. 请记住,除非您提供问题的所有细节,否则无论他们有多少经验,没有人能为您提供帮助。

Did you try the touchstart / touchend / tap events instead of click? 您是否尝试了touchstart / touchend / tap事件而不是click? As far as I know, Apple has been having problems with the click events. 据我所知,Apple在点击事件方面一直存在问题。 Also, click events on mobile devices will have a slower response (a delay of approx 300ms if I remember well) so you're better just using touch specific events. 此外,移动设备上的点击事件的响应速度会较慢(如果我记得很好,则延迟约为300毫秒),因此最好使用特定于触摸的事件。

What are you building? 你在建什么 Is it a mobile web app or? 它是移动网络应用程序还是? Will it run in a standard mobile browser or something like PhoneGap etc? 它可以在标准的移动浏览器或PhoneGap等工具中运行吗?

Update2: UPDATE2:

Ok. 好。 It works as long as the code is not called on Click. 只要未在Click上调用该代码,它就起作用。 This eliminates the possibility of another piece of code replacing your "href" with another value because that code would have to be inside your $('#continue').click(function(){ }); 这样就消除了另一段代码用另一个值替换“ href”的可能性,因为该代码必须位于$('#continue').click(function(){ }); block. 块。

The click event is simulated on a touch phone, that's why the touch events are faster (they are native) and less likely to cause problems. 单击事件是在触摸电话上模拟的,这就是为什么触摸事件更快(它们是本机的)并且不太可能引起问题的原因。 You should also make sure that you return false there and not follow the link, that might be what's replacing your "href". 您还应该确保在此处返回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>

Without this line the link is followed and a refresh occurs killing the current script. 如果没有此行,将跟随该链接,并且刷新将终止当前脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM