用哈希打开模态

时间:2017-10-10 07:17:16

标签: 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>

1 个答案:

答案 0 :(得分:1)

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');
        }
      }
});