为什么必须刷新页面才能使用javascript函数? [英] Why do I have to refresh my page for a javascript function to work?

查看:132
本文介绍了为什么必须刷新页面才能使用javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个移动网站并使用jQuery.

I'm developing a mobile site and using jQuery.

当我加载某个页面并单击所需的按钮时,直到刷新页面后代码才会执行​​.为什么会这样?

When I load a certain page and click the desired button, the code does not execute until I refresh the page. Why is this ?

这就是我的感觉

script.js

$(document).ready(function() 
{ 
  $("#user-save").click(
      function(e)
       {
         alert(/clickity-click/);
       });
});

page.php

<html>
   <head>
       <!-- Other jQuery files here -->
      <script src="script.js"></script>
   </head>
<body>
   <div id="user-save" data-role="button">Click me like a boss</div>
</body>
</html>

推荐答案

JQuery Mobile加载页面的方式与常规" javascript应用程序不同.通常,您将需要绑定到pageinit事件,而不是document.ready.

JQuery Mobile loads pages differently from a 'normal' javascript application. You will typically need to bind to the pageinit event instead of document.ready.

请参见此主题的先前SO讨论

这篇关于为什么必须刷新页面才能使用javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆