Bootstrap Tooltip无法使用JS工作

时间:2017-12-14 17:59:10

标签: javascript css twitter-bootstrap

我正在尝试使引导工具提示在我的应用程序中正常工作,但似乎没有任何工作......它让我有点疯狂......我可以看到工具提示但它看起来不应该......动画不起作用的样式不起作用..我不知道如何解决它..

我在javascript上构建了1-100%的比例...当用户将鼠标悬停在其中一个比例步骤上时,它会显示百分比 - 看一下照片enter image description here

这是我的代码: HTML:

{% load bootstrap %}
{% load static %}
{% load i18n %}
{% load survey_extras %}

<table class="table">
    <!--<thead>
      <tr>
        <th> Question  </th>
        <th> Answers </th>
      </tr>
    </thead> -->
<tbody>
  {% for form in response_form %}
         {% if form.field.widget.attrs.category == category.name or not form.field.widget.attrs.category %}
          <tr class="{% if form.errors%} danger {% endif %}">
              <td>
                <div class="container-fluid">
                  <div class="question-title row">
                    <div class="col-5">
                      <img src="{% static 'images/RightSpeech.jpg' %}" class="img-fluid" alt="Responsive image">
                    </div>
                    <div class="col-7">
                      <h5>{{ form.label|safe }}</h5>
                    </div>
                  </div></div>

                  {% if form.field.required %}
                    <span class="glyphicon glyphicon-asterisk" style="color:red"> </span>
                  {% endif %}
                <span class="help-inline" style="color:red">
                    <strong> {% for error in  form.errors %}{{ error }}{% endfor %} </strong>
                </span> <br>

              <div class="answers">
                  {% for field in form %}
                      <ul>
                        {{ field }}
                      </ul>
                  {% endfor%}

                  <!--{% if "hidden" in form.field.widget.attrs %}-->
                      <p id="check" hidden>{{ type }}</p>
                      {% if "scale" == type %}<br>
                          {% for scale in scales %}
                              {{ scale|safe }}
                          <div id="rate" class="scale"></div>
                          <div class="scale-title">
                              <div class="container">
                                  <div class="row">
                                      <div class="col scaleleft">
                                        0%
                                      </div>
                                      <div class="col scaleright">
                                        100%
                                      </div>
                                  </div>
                                </div>
                          </div>
                          <br>
                          {% endfor %}
                      {% elif 'choice-scale' == type %}
                          <br>
                          {% for scale in scales %}
                              <div class="row questionbox">
                                <div class="col-1">
                                  <input onclick="peace(this)" name="{{ forloop.counter0 }}" type="checkbox" style="margin-right: 5px" class="checkbox">
                                </div>
                                {{ scale|safe }}
                              </div>
                              <br>
                             <div id="rate" class="scale"></div>
                             <div class="scale-title">
                                 <div class="container">
                                     <div class="row">
                                         <div class="col scaleleft">
                                           0%
                                         </div>
                                         <div class="col scaleright">
                                           100%
                                         </div>
                                     </div>
                                   </div>
                             </div>
                              <br><br>
                          {% endfor %}
                      {% endif %}
            <!--  {% endif %} -->
              </div>

            </td>
        </tr>
    {% endif %}
{% endfor %}
    </tbody>
</table>

<script>
// Initialize tooltip component
$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})
// Initialize popover component
$(function () {
  $('[data-toggle="popover"]').popover()
})
$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();
});

</script>

JavaScript:

function createScale(which){
    var index = parseInt(which);
    for(var i = 0; i <= 100; i++) {
        var strg = '<div class="numbers ' + i + ' ' + index + '"' +
            'onclick=\'select(this)\' data-toggle="tooltip" data-placement="top" title= "' + i + '%"></div>';
        classScale[index].innerHTML += strg;
    }
}
我的base.html中的

标题:

<head>
    <meta charset="utf-8">
    <title>SoftScore - Team Recruitment Analytics</title>
    <!-- CSS-->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static '/css/master.css' %}">
    <link href="{% static 'css/widgets.min.css' %}" rel="stylesheet" type="text/css">
    <link href="{% static 'css/index.css' %}" rel="stylesheet">

    <link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.ico'%}"/>
    <link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.ico'%}"/>

    <!-- Fonts-->
    <link href="https://fonts.googleapis.com/css?family=Satisfy" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Ubuntu:300|Ubuntu:300italic" rel="stylesheet">
    <link href="{% static 'vendor/font-awesome/css/font-awesome.min.css' %}" rel="stylesheet" type="text/css">
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Merriweather:400,300,300italic,400italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>

    <!-- Plugin CSS -->
    <link href="{% static "vendor/magnific-popup/magnific-popup.css" %}" rel="stylesheet">

    <!-- Java Script-->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.13.0/umd/popper-utils.js" integrity="sha256-h4TIzOvCoquQKCItCFCPbGMXDe7dbjSQsgZkeS396kA=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>



  </head>

这应该是这样的: enter image description here

编辑img:enter image description here

css:

.scale{
    display: inline-flex;
    border: black 1px solid;
    width:71%;
    margin:auto;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    align-items: center;
    justify-content: center;
}
.numbers{
    width: 5px;
    height: 40px;
    background-color: #66bb9e;
    margin-right: 2px;
}

.numbers:hover {
    background-color: #fa7770;
}

.scale-title{
  display: inline-flex;
  width:71%;
  margin:auto;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}


.scaleleft{
font-size: 0.7rem;
text-align: left;
}

.scaleright{
  font-size: 0.7rem;
  text-align: right;
}

.answers p{
  margin-bottom: 3rem;
}

.scale-title .container .row .col{
padding-right:0;
padding-left: 0;
}

.tooltip-inner {
    background-color:#fa7770;
    margin-bottom: 3px;
    font-weight: 400;
    font-family: Ubuntu;
}

1 个答案:

答案 0 :(得分:3)

您的JavaScript会在DOM之外创建相关的工具提示组件,这意味着您现在的初始化(10^9+7$(function() { })不会影响这些元素。

这是一个非常常见的问题,任何通过AJAX或在DOM之外插入的内容都需要稍微改变一下选择器的初始化方式。

关于Bootstrap,Tooltip组件的一个选项可能是:

$(document).ready()

在上面的代码中,您将组件初始化为现有元素($('body').tooltip({ selector: '[data-toggle="tooltip"]', container: 'body' }); )并声明实际触发Tooltip的选择器。这将影响<body>中的任何元素,无论它是否是初始HTML DOM的一部分。

注意:在上面的示例中,我指定了您可能需要或可能不需要的<body>。您可以在此处阅读有关该特定选项(和其他选项)的更多信息:

https://getbootstrap.com/docs/4.0/components/tooltips/#options