gpt4 book ai didi

javascript - Jquery Mobile 使脚本执行两次

转载 作者:搜寻专家 更新时间:2023-10-31 08:47:32 26 4
gpt4 key购买 nike

为什么 test 消息显示两次:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<script type="text/javascript">
alert("Test");
</script>
</body>
</html>

我从 http://jquerymobile.com/demos/1.2.0/docs/about/getting-started.html 复制了标题当我开始做一些例子时,我注意到我的 javascript 方法被调用了两次。

这是 JsFiddle 中的代码:

http://jsfiddle.net/LsxBW/2/

最佳答案

正如 Explosion Pills 完美表述的那样,您必须遵守 jQM 规则。至少:

  • 使用合适的jQM markup对于一个或多个页面
<div data-role="page" id="page1">
<div data-role="header">
<h1>Page Title</h1>
</div><!-- /header -->
<div data-role="content">
<p>Page content goes here.</p>
</div><!-- /content -->
<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
$(function() {   
alert("Test");
});

使用

$(document).on("pageinit", "#page1", function(){
alert("Test");
});

jsFiddle is here

关于javascript - Jquery Mobile 使脚本执行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14532221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com