简体   繁体   English

如何解决 js/jquery 文件之间的冲突?

[英]How can I solve a conflict between to js/jquery files?

I am building a little website with Django and Bootstrap3 CSS.我正在用 Django 和 Bootstrap3 CSS 建立一个小网站。 So far, everything is fine and I have some menus which use data-toggle for a dropdown, which can also collapse on the second click.到目前为止,一切都很好,我有一些菜单使用 data-toggle 作为下拉菜单,在第二次点击时也可以折叠。 In my base.html I have this code at the end:在我的 base.html 最后我有这个代码:

<!-- Bootstrap core JavaScript
            ================================================== -->
         <!-- Placed at the end of the document so the pages load faster -->
            <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
        <!--<script>window.jQuery || document.write('<script src="../../../boot/js/jquery.min.js"><\/script>')</script>-->
        <script>window.jQuery || document.write('<script src="{% static "boot/js/jquery.min.js" %}"><\/script>')</script>
        <script src="{% static 'boot/js/bootstrap.min.js' %}"></script>

This works for me, even as I am not experienced with js or jquery.这对我有用,即使我对 js 或 jquery 没有经验。

Now I wanted a little widget to add datepicker plus for one of my forms.现在我想要一个小部件来为我的 forms 之一添加 datepicker plus。 And there comes the conflict.冲突就来了。 According to https://pypi.org/project/django-bootstrap-datepicker-plus/ I have to add根据https://pypi.org/project/django-bootstrap-datepicker-plus/我必须添加

{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript jquery='full'%}
{{ my-form.media }}

to my template.到我的模板。 When I do this the bootstrap_css breaks my page design.当我这样做时, bootstrap_css 会破坏我的页面设计。 When I disable that line everything looks fine at first glance.当我禁用该行时,乍一看一切都很好。 And the datepicker widget works.日期选择器小部件有效。 However, the dropdown menus do not work anymore.但是,下拉菜单不再起作用。

I guess that the multiple script includes are responsible for that, but I do not know enough about it, how to solve this.我想多个脚本包含对此负责,但我对此知之甚少,如何解决这个问题。 I have already tried to copy the includes from base.html to the other template and so on, but nothing worked.我已经尝试将包含从 base.html 复制到另一个模板等等,但没有任何效果。 And as soon as I get the dropdown menus to work again, the datepicker widget will stop.一旦我让下拉菜单再次工作,日期选择器小部件就会停止。

I am puzzled because I thought that it would have to do with the includes, but both bootstrap.js seem to have the same content when it comes to data-toggle:我很困惑,因为我认为这与包含有关,但在数据切换方面,两个 bootstrap.js 似乎具有相同的内容:

var r='[data-toggle="dropdown"]',o=function(t){a(t).on("click.bs.dropdown",this.toggle)};

I kind of solved my problem, but I do not really understand the solution.我有点解决了我的问题,但我并不真正理解解决方案。 But I want to post this here.但我想在这里发布这个。 Perhaps it helps someone else, too, and perhaps someone can shed some light on the solution why it works.也许它也对其他人有所帮助,也许有人可以阐明解决方案为什么有效。

After comparing all the duplicate includes and seeing that everything came down to include jquery twice, I found that it's rather not the version (local/remote, new/old) of jquery, which was important, but the position of the include and the number of includes.在比较了所有重复的内容之后,并看到所有内容都包括在内,包括ZD223E143918E478349D52476506C22EZ两次,我发现它不是ZD2223E的版本(local/remote,new/of New/of ZD223E.1439188E478E49D222222222222ERTAN)。的包括。

Now I added现在我添加了

<script src="http://code.jquery.com/jquery-3.5.1.min.js"></script>

and in the footer of the base.html template并在 base.html 模板的页脚

<script>window.jQuery || document.write('<script src="http://code.jquery.com/jquery-3.5.1.min.js"><\/script>')</script>

as I found out that this means to check if jquery was included before and if not to include it.因为我发现这意味着检查 jquery 之前是否包含,如果不包含它。

Other websites and also SO discussions just mentioned the performance of different placings of the include, but I did not find anything that this position was so important.其他网站和 SO 讨论刚刚提到了 include 不同位置的性能,但我没有发现这个 position 如此重要。

However, not the menus work as well as the datepicker plus widget.但是,菜单的效果不如 datepicker plus 小部件。 :) :)

Now I added现在我添加了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM