根据按下的按钮替换弹出文本
Replacing popup text depending on button pressed
下面是创建调用弹出窗口的按钮的代码:
<a href="#positionWindow" data-rel="popup" data-position-to="window" data-transition="flow">popup button</a>
这是使用 jQuery 手机创建弹出窗口的代码:
<div data-role="popup" id="positionWindow" class="ui-content popup" data-theme="b" data-overlay-theme="a">
<a href="#" data-rel="back" data-role="button" data-theme="b" data-overlay-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>
<p> Replace the text here depending on which button is pressed </p>
</div>
如何创建多个按钮,它们都调用用于创建弹出窗口的相同代码,然后根据按下的按钮替换弹出窗口内的文本?
在我看来,只使用一个弹出窗口并动态设置内容似乎很好。不要在标记中使用 href
,您应该从代码打开弹出窗口:
$(document).on("vclick", "a[data-popup]", function(e) {
var cases = [
"You pressed Button 1",
"You pressed Button 2"
],
id = $(this).data("popup");
$("#msg-block").text(cases[id - 1]);
$("#myPopup").popup("option", "transition", "flow").popup("open");
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header" data-position="fixed">
<h2>Header</h2></div>
<div role="main" class="ui-content">
<a class="ui-btn" data-popup="1" href="#">Popup Button 1</a>
<a class="ui-btn" data-popup="2" href="#">Popup Button 2</a>
</div>
<div data-role="popup" id="myPopup" class="ui-content popup" data-theme="b" data-overlay-theme="a">
<a href="#" data-rel="back" data-role="button" data-theme="b" data-overlay-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>
<p id="msg-block"> Replace the text here depending on which button is pressed </p>
</div>
</div>
</body>
</html>
说明:为了将按钮绑定到文本消息,我在我的示例中使用了自定义 data-attribute
,但是 - 如果您愿意 - 您可以只使用按钮的 id
。由你决定。
下面是创建调用弹出窗口的按钮的代码:
<a href="#positionWindow" data-rel="popup" data-position-to="window" data-transition="flow">popup button</a>
这是使用 jQuery 手机创建弹出窗口的代码:
<div data-role="popup" id="positionWindow" class="ui-content popup" data-theme="b" data-overlay-theme="a">
<a href="#" data-rel="back" data-role="button" data-theme="b" data-overlay-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>
<p> Replace the text here depending on which button is pressed </p>
</div>
如何创建多个按钮,它们都调用用于创建弹出窗口的相同代码,然后根据按下的按钮替换弹出窗口内的文本?
在我看来,只使用一个弹出窗口并动态设置内容似乎很好。不要在标记中使用 href
,您应该从代码打开弹出窗口:
$(document).on("vclick", "a[data-popup]", function(e) {
var cases = [
"You pressed Button 1",
"You pressed Button 2"
],
id = $(this).data("popup");
$("#msg-block").text(cases[id - 1]);
$("#myPopup").popup("option", "transition", "flow").popup("open");
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header" data-position="fixed">
<h2>Header</h2></div>
<div role="main" class="ui-content">
<a class="ui-btn" data-popup="1" href="#">Popup Button 1</a>
<a class="ui-btn" data-popup="2" href="#">Popup Button 2</a>
</div>
<div data-role="popup" id="myPopup" class="ui-content popup" data-theme="b" data-overlay-theme="a">
<a href="#" data-rel="back" data-role="button" data-theme="b" data-overlay-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>
<p id="msg-block"> Replace the text here depending on which button is pressed </p>
</div>
</div>
</body>
</html>
说明:为了将按钮绑定到文本消息,我在我的示例中使用了自定义 data-attribute
,但是 - 如果您愿意 - 您可以只使用按钮的 id
。由你决定。