javascript - 如何让html<a>标签自动运行

标签 javascript html

 <div class="modal fade" id="logoutModal" 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">Ready to Leave?</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
        </div>
        <div class="modal-body">Username or Password is incorrect.</div>
        <div class="modal-footer">
          <a class="btn btn-primary" href="login.html">Ok</a>
        </div>
      </div>
    </div>
  </div>

所以我有这个标签,目前是一个按钮。当我单击此按钮时,它会运行一条显示消息。

如何使此标记在页面打开或重新加载时自动运行。

<a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">Logout</a>;

最佳答案

click() 方法对元素执行单击,就像用户手动单击它一样。

在代码中添加 ID:

<a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal" id="example">Logout</a>

并使用js:

document.getElementById('example').click();

关于javascript - 如何让html<a>标签自动运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55217095/

相关文章:

javascript - 在数组上使用过滤器和匹配方法来查找精确匹配

javascript - 可滑动到屏幕的 div

javascript - 在多个 html 页面之间共享一个变量

javascript - 为什么在文本更改事件后滚动 "Up"? (奇怪的 Action )

javascript - 如何修复 jQuery 多个 else if 语句不起作用

javascript - 'drag' 的 highcharts 包装函数

javascript - 如何更改 asp.net 中的 outerHTML 属性

php - 提交表单后如何获得弹出框?

html - CSS 布局在 Firefox 中困惑但在 IE 或 Opera 中没有?

html - CSS div 没有分离