GVKun编程网logo

JavaScript modal 无法打开(javascript打不开)

1

在这篇文章中,我们将为您详细介绍JavaScriptmodal无法打开的内容,并且讨论关于javascript打不开的相关问题。此外,我们还会涉及一些关于angular封装modal,一个modal,

在这篇文章中,我们将为您详细介绍JavaScript modal 无法打开的内容,并且讨论关于javascript打不开的相关问题。此外,我们还会涉及一些关于angular 封装 modal,一个 modal,多次使用、antd modal on modal 点击未打开、Bootstrap Modal Event Not Fired When Modal is Shown/Hidden、Bootstrap Modal 加载页面本身包含另一个 Modal的知识,以帮助您更全面地了解这个主题。

本文目录一览:

JavaScript modal 无法打开(javascript打不开)

如何解决JavaScript modal 无法打开

我正在制作一个笔记应用程序,让您可以选择在单击按钮时以模式查看所述笔记。注释和模态的 HTML 由事件侦听器动态生成。有两种方法可以关闭模​​态,通过单击“X”按钮或单击模态外部。每当只生成一个音符时,该程序就具有完整的功能,但是一旦我生成了第二个音符,代码就会崩溃。一旦发生这种情况,我只能打开生成的第一个音符的模式,但不能关闭它。然后第二个就打不开了。我该如何解决这个问题?

  1. class Input {
  2. constructor(note) {
  3. this.note = note;
  4. }
  5. }
  6. class UI {
  7. addNote(input) {
  8. // Get table body below form
  9. const content = document.querySelector(".content");
  10. // Create tr element
  11. const row = document.createElement("tr");
  12. // Insert new HTML into div
  13. row.innerHTML = `
  14. <td>
  15. ${input.note}
  16. <br><br>
  17. <button class="modalBtn">View Note</button>
  18. </td>
  19. `;
  20. content.appendChild(row);
  21. // Event listener to make modal
  22. document.querySelector(".modalBtn").addEventListener("click",function(e) {
  23. // Get container div
  24. const container = document.querySelector(".container");
  25. // Create div
  26. const div = document.createElement("div");
  27. // Assign class to it
  28. div.className = "modal";
  29. // Insert HTML into div
  30. div.innerHTML = `
  31. <div class="modal-content">
  32. <span class="closeBtn">&times;</span>
  33. <div>
  34. <p>${input.note}</p>
  35. </div>
  36. </div>
  37. `;
  38. // Append the new div to the container div
  39. container.appendChild(div);
  40. // Get modal
  41. const modal = document.querySelector(".modal");
  42. // Event listener to close modal when "x" is clicked
  43. document.querySelector(".closeBtn").addEventListener("click",function() {
  44. container.removeChild(modal);
  45. });
  46. // Event listener to close when the window outside the modal is clicked
  47. window.addEventListener("click",function(e) {
  48. if (e.target === modal) {
  49. container.removeChild(modal);
  50. }
  51. });
  52. });
  53. }
  54. // Clear input field
  55. clearinput() {
  56. note.value = "";
  57. }
  58. }
  59. // Event listener for addNote
  60. document.getElementById("note-form").addEventListener("submit",function(e) {
  61. // Get form value
  62. const note = document.getElementById("note").value;
  63. // Instantiate note
  64. const input = new Input(note);
  65. // Instantiate UI
  66. const ui = new UI();
  67. // Validate form (make sure input is filled)
  68. if (note === "") {
  69. // Error alert
  70. alert("Please fill in text field!");
  71. }
  72. else {
  73. // Add note
  74. ui.addNote(input);
  75. // Clear input field
  76. ui.clearinput();
  77. }
  78. e.preventDefault();
  79. });
  1. h5 {
  2. color: green;
  3. }
  4. .modal {
  5. position: fixed;
  6. z-index: 1;
  7. left: 0;
  8. top: 0;
  9. width: 100%;
  10. height: 100%;
  11. background-color: rgba(0,0.5);
  12. }
  13. .modal-content {
  14. background-color: #fff;
  15. margin: 20% auto;
  16. padding: 30px;
  17. width: 70%;
  18. Box-shadow: 0 5px 8px 0 rgba(0,0.2),0 7px 20px 0 rgba(0,0.17);
  19. animation-name: modalopen;
  20. animation-direction: 1s;
  21. }
  22. .closeBtn {
  23. color: #aaa;
  24. /* float: right; */
  25. font-size: 30px;
  26. margin-bottom: 1rem;
  27. padding-bottom: 1rem;
  28. }
  29. .closeBtn:hover,.closeBtnBtn:focus {
  30. color: #000;
  31. text-decoration: none;
  32. cursor: pointer;
  33. }
  34. .closeBtn + div {
  35. margin-top: 2rem;
  36. }
  37. @keyframes modalopen {
  38. from {
  39. opacity: 0;
  40. }
  41. to {
  42. opacity: 1;
  43. }
  44. }
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <Meta charset="UTF-8">
  5. <Meta name="viewport" content="width=device-width,initial-scale=1.0">
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.css" integrity="sha512-5fsy+3xG8N/1PV5MIJz9ZsWpkltijBI48gBzQ/Z2eVATePGHOkMIn+xTDHIfTZFVb9GMpflF2wOWItqxAP2oLQ==" crossorigin="anonymous" />
  7. <link rel="stylesheet" href="style.css">
  8. <title>Note Taker</title>
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h1>Note Taker</h1>
  13. <h5>Add A New Note:</h5>
  14. <form id="note-form">
  15. <div>
  16. <label>Note:</label>
  17. <textarea name="Note" id="note" class="u-full-width"> </textarea>
  18. </div>
  19. <div>
  20. <button type="submit" class="button-primary">Add Note</button>
  21. </div>
  22. </form>
  23. <table>
  24. <tbody class="content"></tbody>
  25. </table>
  26. </div>
  27. <script src="app.js"></script>
  28. </body>
  29. </html>

解决方法

拳头..我喜欢你的语法!!迄今为止我见过的最好的!第二..你做一个一般查询选择器,不要单独处理它们。这会是个问题吗?

编辑: 由于某些原因,我将重新制定我的答案..

document.querySelector(''class'') 返回一个带有 DOM 元素引用的 Html-Collection,其中包含指定的 html 类,应单独处理。

angular 封装 modal,一个 modal,多次使用

angular 封装 modal,一个 modal,多次使用

js:

app.directive("modal", ["$timeout", function ($timeout) {
        return {
            restrict: "AE",
            templateUrl: "/template/modal.html",
            scope: {
                modalData: "=",
                sureCallback: "&",
                clearSearchTerm: "&"
            },
            link: function (scope, element) {
                var modalHeaderHeight, modalFooterHeight, firstShow = true;

                $("body").on("show.bs.modal", "#" + scope.modalData.modalId, function (e) {
                    if ($(".modal-dialog").length > 1) {
                        $(element).find(".modal-dialog").css("margin", $(".modal-dialog").eq(0).css("margin"));
                    }
                });

                $("body").on("shown.bs.modal", "#" + scope.modalData.modalId, function (e) {
                    $.isFunction(scope.modalData.callback) && scope.modalData.callback();

                    if (firstShow) {
                        firstShow = false;
                    } else {
                        return;
                    }

                    modalHeaderHeight = $(element).find(".modal-header").outerHeight(false);
                    modalFooterHeight = $(element).find(".modal-footer").outerHeight(false);

                    $(element).find(".modal-body").css({
                        maxHeight: "calc(100vh - " + (modalHeaderHeight + modalFooterHeight + 60) + "px )",
                        overflowY: "auto"
                    });
                });

                $("body").on("hide.bs.modal", "#" + scope.modalData.modalId, function (e) {
                    if ($.isFunction(scope.clearSearchTerm)) {
                        scope.clearSearchTerm();
                    }
                });

                $(element).on("click", ":not(.md-select-menu-container)", function (event) {
                    $(".md-select-backdrop").click();
                    event.stopPropagation();
                });
            }
        };
    }]).service("modal", ["$timeout", "$compile", function ($timeout, $compile) {
            this.show = function (selector, scope, callback) {
                angular.element("#" + selector).modal("show");

                if ($.isFunction(callback)) {
                    angular.element("#" + selector).off("shown.bs.modal");
                    angular.element("#" + selector).on("shown.bs.modal", function (e) {
                        $.isFunction(callback) && callback();
                        scope && !scope.

phase && scope.$apply();
                    });
                }
            };

            this.replaceModalBody = function (modalSelector, modalBodyselector, scope) {
                var modalBody = angular.element(modalBodyselector).html();

                modalBody = $compile(modalBody)(scope);
                angular.element(modalSelector).find(".modal-body").html(modalBody);
            };

            this.replaceModalHeader = function (modalSelector, modalHeaderselector, scope) {
                var modalHeader = angular.element(modalHeaderselector).html();

                modalHeader = $compile(modalHeader)(scope);
                angular.element(modalSelector).find(".modal-header").html(modalHeader);
            };
     }]);

    template:

<divid="{{modalData.modalId}}">
  <div>
    <div>
      <div>
        <button type="button"data-dismiss="modal">
          <span aria-hidden="true">&times;</span>
          <span>Close</span>
        </button>
        <h4ng-bind="modalData.modalTitle"></h4>
      </div>
      <divng-bind-html="modalData.modalBodyContent|trusted_html"></div>
      <div>
        <md-button md-colors="{color: ''grey-500''}" data-dismiss="modal">Cancel</md-button>
        <md-button md-colors="{color: ''green-500''}" data-dismiss="modal" ng-click="sureCallback()">Submit</md-button>
      </div>
    </div>
  </div>
</div>

antd modal on modal 点击未打开

如何解决antd modal on modal 点击未打开

我有一个 antd 模态,如下面的代码所示,现在当我选择 Create Manual 并点击 NextI want to close this modal and open another Modal2 但另一个模态没有得到点击下一步后打开。

这是我的代码。 ( CodesandBox 现场演示 - link )

请提出一种解决方法来生成他的第二个模态。谢谢

import React,{ useState } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Modal,Button,Radio } from "antd";

const App = () => {
  const [isModalVisible,setIsModalVisible] = useState(false);
  const [selecTradio,setselecTradio] = useState("preselect");
  const showModal = () => {
    setIsModalVisible(true);
  };
  const select = (e) => {
    // you can save the value in here
    setselecTradio(e.target.value);
    console.log(e.target.value);
  };
  function modalclick() {
    if (selecTradio === "preselect") {
      alert("pre-select");
    } else {
      //---------------> UNABLE TO OPEN ANOTHER MODAL HERE   <-------------------------------------
      <Modal title="Create Test Suite" visible={isModalVisible}>
        MODAL 2 COMES HERE
      </Modal>;
      alert("manual");
    }
  }

  return (
    <>
      <Button type="primary" style={{ float: "right" }} onClick={showModal}>
        Create Test Suite
      </Button>
      <Modal
        title="Create Test Suite"
        visible={isModalVisible}
        footer={[
          <Button key="cancel" onClick={() => setIsModalVisible(false)}>
            Cancel
          </Button>,<Button type="primary" key="next" onClick={modalclick}>
            Next
          </Button>
        ]}
      >
        <Radio.Group
          defaultValue="preselect"
          buttononChange={(e) => {
            select(e);
          }}
        >
          <Radio value="preselect">Create from Preselect</Radio>
          <Radio value="manual">Create Manual</Radio>
        </Radio.Group>
      </Modal>
    </>
  );
};

ReactDOM.render(<App />,document.getElementById("container"));

解决方法

要显示模态 2,您可以使用 useState 钩子useRef 钩子。在这两种方法中,您首先需要将此模态 2 放入“应用程序”的返回

useState 方式:只需使用 state 来控制可见性,就像您在 modal 1 中所做的那样,很简单。

useRef 方式:这有点复杂。您需要在模态组件内部使用 useImperativeHandle,并创建一个函数(也在内部)来控制可见性。因此,在您的页面中,您只需调用组件内部的函数即可显示模态。使用这种方法,可见性状态控制的逻辑离开页面,进入组件。

Bootstrap Modal Event Not Fired When Modal is Shown/Hidden

问题描述

使用jquery(3.2.1)。

Bootstrap(v4-alpha)的Modal中,如果Modal存在.fade那么shown.bs.modal以及hidden.bs.modal事件的回调函数不会被正常触发。

但是show.bs.modal以及hide.bs.modal两个事件的触发是正常的。

这两种事件的区别在于immediatelywait for CSS transitions to complete

解决方法

在设置事件监听器之前动态添加.fadeclass:

$ ->
    registerModal = $(''#registerModal'')
    registerModal.addClass(''fade'')
    registerModal.on(''hidden.bs.modal'', ->
        $(this).find(''input'').val('''')
    )
登录后复制

以上就是Bootstrap Modal Event Not Fired When Modal is Shown/Hidden的详细内容,更多请关注php中文网其它相关文章!

Bootstrap Modal 加载页面本身包含另一个 Modal

如何解决Bootstrap Modal 加载页面本身包含另一个 Modal

我是 Bootstrap 的新手,对以下描述的功能有疑问: 我有第 1 页通过按钮打开引导模式。

<button type="button"data-toggle="modal" data-target="#exampleModal" data-modal-url="./test2.html">Modal Launch</button>

<divid="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="static">
    <divrole="document">
        <div>
            <div>
                <h5id="exampleModalLabel">Example Modal</h5>
                <button type="button"data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button>
            </div>
            <div>
                ...
            </div>
            <div>
                <button type="button"data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
$(''#exampleModal'').on(''show.bs.modal'',function (event) {
        var src = $(this);
        var modalUrl = src.data(''modal-url'');
        src.find(''.modal-body'').load(modalUrl);
    });

所以当我点击按钮引导模式弹出。我的问题在这里,现在是否可以为这个例子加载的页面说 test2.html 有自己的模式弹出功能? 我本质上想看到的是,是否有可能让模态加载一个页面,然后有按钮打开另一个模态来加载页面。然后最终使用 jQuery UI 之类的东西独立地拖动它们。

关于JavaScript modal 无法打开javascript打不开的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于angular 封装 modal,一个 modal,多次使用、antd modal on modal 点击未打开、Bootstrap Modal Event Not Fired When Modal is Shown/Hidden、Bootstrap Modal 加载页面本身包含另一个 Modal的相关信息,请在本站寻找。

本文标签:

上一篇如何一次打开多个标签页 [JavaScript](一键打开多个页面)

下一篇javascript 中可序列化、可恢复的函数调用,用于跨重启的有状态异步执行