今天写移动端的页面,碰到一个怪事:
页面上有一个Button,但这个Button绑定的的onclick事件总不会执行,发现需要刷新一下页面后点击才能触发。我的页面HTML代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="jq/jquery.mobile-1.3.2.min.css" />
<script src="jq/jquery.min183.js"></script>
<script src="jq/jquery.mobile-1.3.2.min.js"></script>
<link rel="Stylesheet" href="Style.css" />
<script type="text/javascript">
$(document).ready(function () {
$('#btnOrder').click(function (e) {
$.get("DoCart.asp?PID=" + 515, function (data, status) {
if (status == "success") {
alert("Data: " + data)
self.location = "buy.asp";
}
})
});
});
</script>
</head>
<body>
<div data-role="page">
<div data-role="header" data-position="fixed" data-theme="e">
<a href="product.asp?SID=9" data-role="button" data-icon="arrow-l">返回</a>
<h1>北极绒秋衣秋裤</h1>
<a style="margin-top:2.3px" id="main_exit" data-icon="cart-icon" data-iconpos="left" data-shadow="false" data-iconshadow="false" class="ui-btn-right">购物车<span style="color:Red;">0</span></a>
</div>
<div data-role="content" data-theme="e">
<h3>北极绒秋衣秋裤<span style="color:Red;">¥89</span></h3>
<p>北极绒 秋衣秋裤 男女 莫代尔 男麻灰 L北极绒,品质保证!</p>
<p><input type="button" value="立即订购" id="btnOrder" /></p>
(后面省略了)
刚开始以为是手机浏览器缓存的旧页面,于是反复清空缓存,换不同的浏览器(360浏览器、UC浏览器)测试都有这个问题,后来在PC上用Chrome试也这样。
疑惑之下,使用Chrome的开发者工具:elements查看,文档变成了这样:
<html xmlns="http://www.w3.org/1999/xhtml" class="ui-mobile">
<head><base href="http://shop.zldb.cc/m/Item.asp?PID=515&SID=9">
<title>北极绒秋衣秋裤</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="jq/jquery.mobile-1.3.2.min.css">
<link rel="Stylesheet" href="Style.css">
<script src="jq/jquery.min183.js"></script>
<script src="jq/jquery.mobile-1.3.2.min.js"></script>
</head>
<body class="ui-mobile-viewport ui-overlay-c">
<div data-role="page" data-url="/m/buy.asp" tabindex="0" class="ui-page ui-body-c ui-page-header-fixed" style="padding-top: 44px; min-height: 588px;">
<div>
<div class="ui-input-text ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-c"><input id="Text1" type="text" class="ui-input-text ui-body-c"></div>
</div>
</div>
<div class="ui-loader ui-corner-all ui-body-a ui-loader-default"><span class="ui-icon ui-icon-loading"></span><h1>loading</h1></div><div data-role="page" data-url="/m/Item.asp?PID=515&SID=9" data-external-page="true" tabindex="0" class="ui-page ui-body-c ui-page-header-fixed ui-page-active" style="padding-top: 44px; min-height: 288px;">
<div data-role="header" data-position="fixed" data-theme="e" class="ui-header ui-bar-e ui-header-fixed slidedown" role="banner">
<a href="product.asp?SID=9" data-role="button" data-icon="arrow-l" class="ui-btn-left ui-btn ui-btn-up-e ui-shadow ui-btn-corner-all ui-btn-icon-left" data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="e"><span class="ui-btn-inner"><span class="ui-btn-text">返回</span><span class="ui-icon ui-icon-arrow-l ui-icon-shadow"> </span></span></a>
<h1 class="ui-title" role="heading" aria-level="1">北极绒秋衣秋裤</h1>
<a style="margin-top:2.3px" id="main_exit" data-icon="cart-icon" data-iconpos="left" data-shadow="false" data-iconshadow="false" class="ui-btn-right ui-btn ui-btn-up-e ui-btn-corner-all ui-btn-icon-left" data-corners="true" data-wrapperels="span" data-theme="e"><span class="ui-btn-inner"><span class="ui-btn-text">购物车<span style="color:Red;">0</span></span><span class="ui-icon ui-icon-cart-icon"> </span></span></a>
</div>
<div data-role="content" data-theme="e" class="ui-content ui-body-e" role="main">
<h3>北极绒秋衣秋裤<span style="color:Red;">¥89</span></h3>
<p>北极绒 秋衣秋裤 男女 莫代尔 男麻灰 L北极绒,品质保证!</p>
<p><div data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="e" data-disabled="false" class="ui-btn ui-shadow ui-btn-corner-all ui-btn-up-e" aria-disabled="false"><span class="ui-btn-inner"><span class="ui-btn-text">立即订购</span></span><input type="button" value="立即订购" id="btnOrder" class="ui-btn-hidden" data-disabled="false"></div></p>
看来:
1、JQuery-Mobile 的init初始化网页会对原有结构做了调整
2、从为DIV增加了属性data-url来看,Page DIV中的内容可能是异步加载的
所以猜JS代码放在<head>中时,DIV中的DOM对象还没有生成,所以事件也没有绑定
把JS代码移到<body>中的<div data-role="page">部分:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<title></title> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" /> | |
<link rel="stylesheet" href="jq/jquery.mobile-1.3.2.min.css" /> | |
<script src="jq/jquery.min183.js"></script> | |
<script src="jq/jquery.mobile-1.3.2.min.js"></script> | |
<link rel="Stylesheet" href="Style.css" /> | |
</head> | |
<body> | |
<div data-role="page"> | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
$('#btnOrder').click(function (e) { | |
$.get("DoCart.asp?PID=" + 515, function (data, status) { | |
if (status == "success") { | |
alert("Data: " + data) | |
self.location = "buy.asp"; | |
} | |
}) | |
}); | |
}); | |
</script> | |
<div data-role="header" data-position="fixed" data-theme="e"> | |
<a href="product.asp?SID=9" data-role="button" data-icon="arrow-l">返回</a> | |
<h1>北极绒秋衣秋裤</h1> | |
<a style="margin-top:2.3px" id="main_exit" data-icon="cart-icon" data-iconpos="left" data-shadow="false" data-iconshadow="false" class="ui-btn-right">购物车<span style="color:Red;">0</span></a> | |
</div> | |
<div data-role="content" data-theme="e"> | |
<h3>北极绒秋衣秋裤<span style="color:Red;">¥89</span></h3> | |
<p>北极绒 秋衣秋裤 男女 莫代尔 男麻灰 L北极绒,品质保证!</p> | |
<p><input type="button" value="立即订购" id="btnOrder" /></p>
|
终于解决了!
相关推荐
首先,要在项目中使用 jQuery Mobile,你需要在 HTML 文件的 `<head>` 标签内引入 jQuery 和 jQuery Mobile 的 CSS 和 JS 文件。通常,这可以通过 CDN(内容分发网络)链接实现,如下所示: ```html <!DOCTYPE html...
最后,不要忘记在页面加载完成后初始化jQuery Mobile,这通常放在`<body>`标签的末尾: ```html <script> $(document).on("mobileinit", function () { // 自定义jQuery Mobile设置 }); $(document).ready...
本篇文章将深入探讨如何创建一个简单的 jQuery Mobile 应用,以及在实际开发中的基本使用方法。 首先,让我们理解 jQuery Mobile 的核心概念。jQuery Mobile 基于页面架构,而不是传统的单一页面应用。这意味着,...
这个资源包以“Hello Word”为例,包含了一组基本的部署文件,用于展示如何在实际项目中运用jQuery Mobile。 首先,我们需要理解jQuery Mobile的基本概念。它是HTML5开发的一个重要工具,支持多种移动平台,如iOS、...
4. **添加组件**:根据需求插入相应的jQuery Mobile组件标签,如`<a data-role="button">`。 5. **事件绑定**:利用jQuery的事件处理函数(如`.on()`)绑定触摸事件和页面事件,如`pageinit`和`pageshow`。 6. **动态...
- 表单组件如`<input>`, `<select>`, `<textarea>`等在jQuery Mobile中会自动美化,提供触摸优化的样式和行为。 - `data-mini="true"`和`data-inline="true"`可以创建小型或内联表单元素。 - `data-clear-btn=...
<script src="jquery.mobile.min.js"></script> </head> <body> <div data-role="page" id="page1"> <!-- 页面内容 --> </div> <div data-role="page" id="page2"> <!-- 另一个页面内容 --> </div> </body> </...
要开始使用 jQuery Mobile,你需要在 HTML 页面的 `<head>` 标签内引入这些文件,通常是按照 CSS -> jQuery -> jQuery Mobile 的顺序: ```html <!DOCTYPE html> <html> <head> <link rel="stylesheet" href=...
2. **JavaScript/jQuery代码**:在`get_mobile_local.js`文件中,我们需要监听手机号输入框的事件,例如`input`或`change`事件。当用户输入发生变化时,使用Ajax调用淘宝的API接口,查询手机号归属地。 ```...
总结起来,jQuery Mobile的外部链接切换功能通过在`<a>`标签中添加`rel="external"`属性实现,这使得开发者可以轻松地在应用内部和外部资源之间建立链接,同时保持良好的用户界面和交互体验。理解并熟练运用这一特性...
这是因为 jQuery Mobile 在进行 AJAX 跳转时,只会加载目标页面中的 `<page>` 内容,而不会加载 `<page>` 标签之外的任何资源。 #### 原理 jQuery Mobile 使用 AJAX 加载新页面时,只加载 `<page>` 标签内的内容,...
在这个主题中,我们将深入探讨jQuery Mobile的核心概念、主要功能以及如何使用其JavaScript(jquerymobile.js)和CSS(jquerymobile.css)文件。 首先,jQuery Mobile提供了一套完整的组件,包括可滚动的列表视图、...
4. **引入资源**:在HTML文件中添加 `jQuery` 和 `jQuery Mobile` 的链接,通常位于 `<head>` 标签内,例如: ```html <script src="bower_components/jquery/dist/jquery.min.js"></script> <link rel=...
在Web开发中,jQuery Mobile和Ajax是两个非常重要的工具,特别是在构建移动应用或者增强网页交互性时。jQuery Mobile是一款强大的框架,它优化了移动设备上的HTML5应用,提供了丰富的组件和触摸友好的用户界面。而...
<option>学生</option><option>一年</option><option>二年</option><option>三年</option><option>三年到五年</option><option>五年到十年</option><option>十年以上</option> </select> </div> ...
在这个场景中,你希望在`<ul>`(无序列表)的`<li>`(列表项)中添加自定义图标,这可以通过CSS3和jQuery Mobile的特性来实现。下面将详细讲解如何操作。 首先,确保你已经引入了jQuery Mobile 1.4.2所需的CSS和...
这意味着将所有必要的样式表(`.css`文件)链接和脚本(`.js`文件)的引用复制到目标页面的`<head>`部分,或者直接将CSS样式和JavaScript代码写入`<style>`和`<script>`标签中,然后把这些标签放置在`<page>`标签内...
因此,位于<head>标签中的外部JS文件和不在<page>标签内的JS代码片段在首次页面加载时不会执行,只有在手动刷新页面后它们才会被正确加载。 这个问题的核心在于理解jQuery Mobile的页面事件和页面生命周期。jQuery ...
HTML5App:在UIWebView中显示JQuery Mobile的示例源代码 HTML5App是一个将HTML5技术与JQuery Mobile框架结合,用于在iOS设备上的UIWebView中构建移动应用的项目。UIWebView是iOS SDK中的一个组件,允许开发者在原生...
为了进一步提高性能和用户体验,你还可以利用jQuery Mobile的页面事件,如`pageinit`、`pagebeforeshow`和`pageshow`,在页面生命周期的不同阶段执行特定的JavaScript代码。同时,可以使用`$.mobile荷载事件`(如`...