首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使html <a>标记自动运行

如何使html <a>标记自动运行
EN

Stack Overflow用户
提问于 2019-03-18 16:18:56
回答 4查看 942关注 0票数 0

代码语言:javascript
运行
复制
 <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>

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

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

代码语言:javascript
运行
复制
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">Logout</a>;

EN

回答 4

Stack Overflow用户

发布于 2019-03-18 16:26:18

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

在你的代码中添加id:

代码语言:javascript
运行
复制
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal" id="example">Logout</a>

并使用js:

代码语言:javascript
运行
复制
document.getElementById('example').click();
票数 1
EN

Stack Overflow用户

发布于 2019-03-18 16:23:26

您可以在页面加载后使用触发器

代码语言:javascript
运行
复制
$( document ).ready(function() {
    $(".dropdown-item").trigger( "click" );
});
票数 0
EN

Stack Overflow用户

发布于 2019-03-18 16:55:27

基于你上面的评论...

我正在尝试做一个表单,如果用户输入错误的凭据,模式将弹出。

...maybe这会有帮助的。如果不是,那么你需要在你的问题中澄清你想要做什么。

在下面的代码片段中,我有一个登录表单。当输入了错误的凭据时,将出现模式。

代码语言:javascript
运行
复制
$("#login").click(function () {
    // Here you would do your check for correct credentials
    // Obviously you wouldn't want to do this in JS in production
    // I'm assuming there would be an AJAX request here that would validate the user's credentials
    // If valid, proceed. If not, call the modal.
    if ($(username).val() !== "blah" || $(password).val() !== "blah") {
  	// Username or password incorrect, show modal
    $("#modal").modal();
  } else {
  	alert("Login successful!");
  }
})
代码语言:javascript
运行
复制
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>


Username: <input id="username" type="text" />
Password: <input id="password" type="password" />
<button type="button" id="login" class="btn btn-info">Login</button>

<div class="modal fade" id="modal" 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" data-dismiss="modal">Try again</a>
      </div>
    </div>
  </div>
</div>

基本上,任何时候你想让模式自动出现(也就是,不用点击任何东西),就像这样调用它:

代码语言:javascript
运行
复制
$("#idOfModal").modal();

如果您想以编程方式隐藏或取消模态,可以通过将hide传递给modal函数来实现:

代码语言:javascript
运行
复制
$("#idOfModal').modal("hide");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55217095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档