我在我的Select2插件上使用WordPress,我需要隔离我加载的版本,以避免与使用Select2的其他插件发生冲突。
我发现了这个答案 by @Kevin Brown,他建议在加载之后,在删除变量之前将select2函数保存到变量中,以避免与其他负载有关的问题:
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script>
<script>
var myOwnSelect2 = $.fn.select2;
delete $.fn.select2;
</script>我遇到的问题是,它在编辑-post.php页面中工作,但在其他任何地方都不行。我的意思是,在Edt-post.php页面上,"select“标记/元素正在被替换,而不是在我的插件设置页面中。每个页面上的代码如下所示:
编辑-post.php
HTML
<div class="myplugin-metabox">
...
<select name="_myplugin_item" class="searchable select2-hidden-accessible" data-placeholder="Select an item..." tabindex="-1" aria-hidden="true">
<option>...</option>
</select>
...
</div>
Javascript
myOwnSelect2.call( $('.myplugin-metabox select'), { dropdownAutoWidth: true, minimumResultsForSearch: Infinity } );admin.php?page=myplugin
HTML
<div id="myplugin-settings">
...
<select id="option-1" name="myplugin-option-1" class="">
<option value="all">...</option>
...
</select>
...
</div>
Javascript
myOwnSelect2.call( $('#myplugin-settings select'), { dropdownAutoWidth: true, minimumResultsForSearch: Infinity } );如果我不使用任何隔离方法,插件中的所有东西都能正常工作.但是我需要隔离,因为许多其他流行的插件都会加载自己的Select2版本/副本。
Javascript初始化是在同一个文件上进行的。保持隔离,如果删除对Edt-post.php选择的初始化,插件设置页面上的选择不会被转换为Select2下拉列表。
对我做错了什么有什么建议吗?
谢谢!
发布于 2018-06-04 08:57:49
我自己找到了问题的原因。我把它写在这里,这样在我的情况下可能会对其他人有所帮助。
问题是,我将jQuery引用为"$",而我本应将其引用为"jQuery“。请查看下面的代码:
没有工作(在我的例子中):
<script>
var myOwnSelect2 = $.fn.select2;
delete $.fn.select2;
</script>确实工作(在我的例子中):
<script>
var myOwnSelect2 = jQuery.fn.select2;
delete jQuery.fn.select2;
</script>https://stackoverflow.com/questions/50554862
复制相似问题