根据按下的按钮替换弹出文本

时间:2017-12-15 16:27:24

标签: javascript html css jquery-mobile

以下是创建调用弹出窗口的按钮的代码:

funcs = {
    funcA: (1, 2, 3), 
    funcB: ({'a' : 1}, [1, 2, 3]),
    funcC: None
}

for func, args in funcs.items(): 
     if args is None:
         func() 
     else:
         func(*args)

这是使用jQuery Mobile创建弹出窗口的代码:

<a href="#positionWindow" data-rel="popup" data-position-to="window" data-transition="flow">popup button</a>

如何创建多个按钮,这些按钮全部调用用于创建弹出窗口的相同代码,然后根据按下的按钮替换弹出窗口内的文本?

1 个答案:

答案 0 :(得分:0)

我觉得只使用一个弹出窗口并动态设置内容。您应该从代码中打开弹出窗口,而不是在标记中使用href

&#13;
&#13;
$(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");
});
&#13;
<!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>
&#13;
&#13;
&#13;

说明:要将按钮绑定到短信,我在我的示例中使用自定义data-attribute但是 - 如果您愿意 - 您只能使用按钮的id。由你决定。

相关问题