html - 指向 Bootstrap 模式打开的 anchor

标签 html twitter-bootstrap anchor

我想打开一个帮助模式,但是根据点击的链接,我需要像在常见问题解答页面上那样关注特定的 anchor 。 在页面上,我们将在 href 链接上添加 #myAmazingTitle(例如 <a href="http://....#myAmazingTitle">FAQ</a> 以将显示集中在特定的 dom 元素上。

<h1>My first title</h1>
<h1 id="myAmazingTitle">My Amazing Title</h1> <== Display will be focused on this title
....

我希望使用 Bootstrap 模态具有相同的行为。

这可能吗?如何实现?

这是 Fiddle对于这个问题。

最佳答案

使用relatedTarget和 HTML data-* 属性来指定目标元素选择器(或 anchor )并将此值发送到 shown.bs.modal 事件的处理程序。请参阅下面的用法示例。

$('#exampleModal').on('shown.bs.modal', function(e) {
  var recipient = $(e.relatedTarget).data('focus');
  $(this).find( recipient ).focus();  
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <span class="modal-title" id="exampleModalLabel">Registration form</span>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="EmailInput">Email address</label>
            <input type="email" class="form-control" id="EmailInput" placeholder="Email">
          </div>
          <div class="form-group">
            <label for="PassInput">Password</label>
            <input type="password" class="form-control" id="PassInput" placeholder="Password">
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Register</button>
      </div>
    </div>
  </div>
</div>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-focus="#EmailInput">Type Email</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-focus="#PassInput">Type Password</button>

关于html - 指向 Bootstrap 模式打开的 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45231043/

相关文章:

javascript - 将 jQuery 代码转换为原生 Javascript

html - 是否建议使用像 .text-center 这样的简单 CSS 类?

html - Bootstrap 表每行有两个不同长度的水平列

menu - 带 anchor 的 Typo3 菜单

c# - Panel 在继承的 Windows 窗体上的行为不正确?

javascript - 如何创建同步轮播

javascript - 为什么悬停时图像偶尔会损坏

html - 带有 Angular 组件的 Flexbox 布局

html - 悬停在主导航下方的子导航栏

html - 在特定列加载 html 表