JS:在表

时间:2017-06-05 06:01:05

标签: javascript jquery html css

我有一张包含某些TR的表格。 我想要的是,当我点击任何tr然后打开一个弹出窗口。但是我无法操纵它的位置到上面点击它的上面。



var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("tr_1");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close_div")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
&#13;
.container {
    margin-top: 10%
}

.container .m-t {
    margin-top: 15px
}

.container .m-t-xs {
    margin-top: 5px
}

.container .m-t-sm {
    margin-top: 10px
}

.container .cursor_pointer {
    cursor: pointer
}

.container .v-middle {
    vertical-align: middle
}

.container .table_div {
    border: thin red solid
}

.container .modal {
    display: none;
    position: fixed;
    z-index: 1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: #000;
    background-color: rgba(0, 0, 0, 0.4)
}

.container .modal-content {
    background-color: #fefefe;
    margin: 0 30px;
    padding: 20px;
    border: 1px solid #888;
    width: 100%;
}

.container .modal-content .form-group label {
    font-size: 12px;
    font-weight: 300
}

.container .modal-content:after {
    content: '';
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin: 0 auto;
    width: 0;
    height: 0;
    border-top: solid 10px #fff;
    border-left: solid 10px transparent;
    border-right: solid 10px transparent
}

.container .close_div {
    width: 20px;
    height: 20px;
    position: absolute;
    border-radius: 50px;
    border: thin white solid;
    top: -7px;
    right: -7px;
    text-align: center;
    background: #000000
}

.container .close_div span {
    color: white;
    position: absolute;
    left: 3px;
    top: -1px
}
&#13;
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link href="CSS/style.css" type="text/css" rel="stylesheet">

  <body>
    <div class="container">
      <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
          <div class="table_div table-responsive">
            <table class="table">
              <thead>
                <th>All</th>
                <th>Name</th>
                <th>Department</th>
              </thead>
              <tbody>
                <tr class="cursor_pointer" id="tr_1">
                  <td>
                    <input type="checkbox" name="">
                  </td>
                  <td>
                    Bradley Stephen
                  </td>
                  <td>
                    Sales
                  </td>
                </tr>

                <tr class="cursor_pointer">
                  <td>
                    <input type="checkbox" name="">
                  </td>
                  <td>
                    John Doe
                  </td>
                  <td>
                    Marketing
                  </td>
                </tr>
              </tbody>
            </table>
          </div>

          <div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="close_div cursor_pointer">
      <span>&#x2715;</span>
    </div>
    <div class="modal_body">
      <div class="form-group">
        <div class="row">
          <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
              <label>First Name</label>
              <input type="text" name="first_name" value="" class="form-control">
          </div>
        </div>

        <div class="row m-t">
          <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
              <label>Last Name</label>
              <input type="text" name="last_name" value="" class="form-control">
          </div>
          <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
            <label for=""></label>
            <button type="button" name="button" class="m-t-xs">Update</button>
          </div>
        </div>
      </div>
    </div>
  </div>

</div>

        </div>
      </div>
    </div>

  </body>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <script type="text/javascript" src="JS/script.js">

  </script>
</html>
&#13;
&#13;
&#13;

当我点击tr中的任何一个时,弹出框应位于相应的tr上方,并使用底部三角形指向tr。

我搜索了很多,但它对我不起作用。 任何帮助都会很棒。 谢谢。

1 个答案:

答案 0 :(得分:1)

使用bootstrap popover而不是模态。连同链接中的解释内容,在tr的click函数中,将放置参数传递给popover方法,如下所示:

$('tr').popover({
         content: //Your Html with textbox and button
         , container: 'body'
         , placement: 'auto top'
         , html: true
      });
$(selector).popover('show');

更新:下面给出了完整的工作html,删除了整个bootstrap模式html并将展示位置更改为自动顶部。如果上面没有空格,则使用“自动”,弹出窗口将显示在下方。

<!DOCTYPE html>
<html>

<head>
   <meta charset="utf-8">
   <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://code.jquery.com/jquery-3.2.1.min.js"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
      crossorigin="anonymous"></script>
   <title>Bootstrap Popover</title>
   <style>
      .container .table_div {
         border: thin red solid
      }

      header {
         padding-top: 160px;
      }
   </style>
   <script>
      $(document).ready(function () {
         var html = '<div class="form-group"><div class="row"><div class="col-lg-8 col-md-9 col-sm-9 col-xs-9"><label>First Name</label><input type="text" name="first_name" value="" class="form-control"></div></div><div class="row m-t"><div class="col-lg-8 col-md-9 col-sm-9 col-xs-9"><label>Last Name</label><input type="text" name="last_name" value="" class="form-control"></div><div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"><label for=""></label><button type="button" name="button" class="m-t-xs">Update</button></div></div></div>'
         $('tbody tr').attr({
            "data-toggle": 'popover'
            , "data-placement": 'auto top'
            , "data-trigger": 'click'
            , "data-html": 'true'
            , "title": function () {
               return $(this).children('td:nth-of-type(2)').html();
            }
            , "data-content": html
            , "data-container": 'body'
         });

         $('tbody tr').popover();
         $('tbody tr').on('show.bs.popover', function () {
            $('tbody tr').not(this).popover('hide');
         });
      });
   </script>
</head>

<body>
   <header></header>
   <div class="container">
      <div class="row">
         <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <div class="table_div table-responsive">
               <table class="table">
                  <thead>
                     <th>All</th>
                     <th>Name</th>
                     <th>Department</th>
                  </thead>
                  <tbody>
                     <tr class="cursor_pointer" id="tr_1">
                        <td>
                           <input type="checkbox" name="">
                        </td>
                        <td>
                           Bradley Stephen
                        </td>
                        <td>
                           Sales
                        </td>
                     </tr>

                     <tr class="cursor_pointer">
                        <td>
                           <input type="checkbox" name="">
                        </td>
                        <td>
                           John Doe
                        </td>
                        <td>
                           Marketing
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
         </div>
      </div>
   </div>
</body>

</html>