Javascript 如何使用按钮单击打开引导模式

Javascript 如何使用按钮单击打开引导模式,javascript,html,Javascript,Html,我开发了一个简单的js猪游戏。你可以在这里看到: 首先,它将打开一个引导模式框来设置一个获胜分数。现在,游戏将通过点击掷骰子按钮开始 好吧,现在如果你赢了,你可以按“新建游戏”按钮重新玩游戏。在这里,当我点击新游戏按钮时,我想再次打开引导模式。为此,我编写了以下代码: document.querySelector(".btn-new-game").addEventListener("click", function () { init(); document.getEl

我开发了一个简单的js猪游戏。你可以在这里看到:

首先,它将打开一个引导模式框来设置一个获胜分数。现在,游戏将通过点击掷骰子按钮开始

好吧,现在如果你赢了,你可以按“新建游戏”按钮重新玩游戏。在这里,当我点击新游戏按钮时,我想再次打开引导模式。为此,我编写了以下代码:

document.querySelector(".btn-new-game").addEventListener("click", function () {
    init();     
    document.getElementById("myModal").modal('show');       
});
但它在控制台日志上显示了一条错误消息:

Uncaught TypeError: document.getElementById(...).modal is not a function
也许这是因为Javascript生活。我不太清楚:

我还尝试了以下代码:

document.getElementById("myModal").click();     
但是没有运气:

我的HTML和js代码有点长,所以我没有放在这里。你可以在这里找到它:


谢谢

你可以这样称呼它:


document.getElementByIdmyModal.modal

您希望从JS打开模态的具体原因是什么

对于Bootstrap 4方法,请尝试以下方法:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

这样修改按钮, 它工作得非常好

<button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>

$myModal.modal'show';您可以使用$myModal.modal'open'$myModal.modal'show'$模态“切换”;然后它显示了这个错误:bootstrap.min.js:6 UncaughtTypeError:f[b]不是一个函数您已经在使用Jquery在窗口加载时使用了model,就像您可以在单击按钮时打开model一样。document.querySelector.btn-new-game.addEventListenerclick,函数{init;$'myModal'.modal'show';};请用我添加的代码检查这把小提琴,比如$'myModal'.modal'show';它的工作-然后它向我显示:TypeError:document.getElementById…modal不是一个函数