javascript - 使用哈希打开模式

标签 javascript jquery modal-dialog bootstrap-modal hashtag

我想打开一个带有哈希的模式(例如:www.example.com/#example。)这是我下面的代码,但模式没有显示。感谢您的回答,我很感激。

$(document).ready(function() {
    $('.popup').click(openModalPopupClick);

          function openModalPopup(){
            var hashText = window.location.hash.substr();
            if (hashText){
              $('#'+hashText).modal('show');
            }
          }

          function openModalPopupClick(){
            var hashText = $(this).attr('href');
            if (hashText){
              $(hashText).modal('show');
            }
          }
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<a href="#regis-new-company" class="popup" role="button">click</a>
<div class="modal fade" id="regis-new-company" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
       <div class="modal-header"></div>
       <div class="modal-body"></div>
       <div class="modal-footer"></div>
    </div>
  </div>    
</div>

最佳答案

hashText 已经有了 # 所以 $('#'+hashText).modal('show'); 应该是 $(hashText).modal( 'show'); 并在就绪时调用 openModalPopupClick 函数。我已经更新了你的代码。

$(document).ready(function() {
$('.popup').click(openModalPopupClick);

      function openModalPopup(){
        var hashText = window.location.hash.substr();
        if (hashText){
          $(hashText).modal('show');
        }
      }

      openModalPopup();

      function openModalPopupClick(){
        var hashText = $(this).attr('href');
        if (hashText){
          $(hashText).modal('show');
        }
      }
});

关于javascript - 使用哈希打开模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46660702/

相关文章:

javascript - 根据 if 语句结果更改 if 语句参数中的变量值

javascript - 如何在数据格式的 ng-repeat 中使用 order 和 group by

javascript - 如何使用不同的颜色使用 CSS 填充 chrome 中输入[类型 ="range"] 的左侧?

jquery - 需要一个支持 $.live() 和分组的 jQuery Lightbox 插件

javascript - jquery搜索框和按钮

javascript - Rails 使用模态和局部变量渲染部分内容

javascript - 仅在特定部分启动 javascript 函数

php - Fancybox - 重新加载页面或加载新页面

用于嵌套模式弹出窗口的 Jquery 插件?

twitter-bootstrap - 如何避免在 Bootstrap 中关闭模式