我必须在我的代码中使用两个jQuery api," jquery-1.8.3.js"one_answers" jquery-ui.js"。但是已经有较旧版本的jQuery。因此,该表格无法正常工作。
所以我在网上搜索解决方案并找到了,我们可以使用
<script>
var jq13 = jQuery.noConflict(true);
</script>
但是我必须使用API,所以我创建了两个变量,但仍然无法正常工作。这是我的代码: -
<script>
(function( $ ) {
$.widget( "ui.combobox", {
_create: function() {
var input,
that = this,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "",
wrapper = this.wrapper = $( "<span>" )
.addClass( "ui-combobox" )
.insertAfter( select );
function removeIfInvalid(element) {
var value = $( element ).val(),
matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
valid = false;
select.children( "option" ).each(function() {
if ( $( this ).text().match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// remove invalid value, as it didn't match anything
$( element )
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
select.val( "" );
setTimeout(function() {
input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
input.data( "autocomplete" ).term = "";
return false;
}
}
input = $( "<input>" )
.appendTo( wrapper )
.val( value )
.attr( "title", "" )
.addClass( "ui-combobox-input" )
.autocomplete({
delay: 0,
minLength: 0,
source: function( request, response ) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
response( select.children( "option" ).map(function() {
var text = $( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) )
return {
label: text.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
$.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi"
), "<strong>$1</strong>" ),
value: text,
option: this
};
}) );
},
select: function( event, ui ) {
ui.item.option.selected = true;
that._trigger( "selected", event, {
item: ui.item.option
});
},
change: function( event, ui ) {
if ( !ui.item )
return removeIfInvalid( this );
}
})
.addClass( "ui-widget ui-widget-content ui-corner-left" );
input.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
};
$( "<a>" )
.attr( "tabIndex", -1 )
//.attr( "title", "Show All Items" )
.tooltip()
.appendTo( wrapper )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "ui-corner-right ui-combobox-toggle" )
.click(function() {
// close if already visible
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
input.autocomplete( "close" );
removeIfInvalid( input );
return;
}
// work around a bug (likely same cause as #5265)
$( this ).blur();
// pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" );
input.focus();
});
},
destroy: function() {
this.wrapper.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
})( jQuery );
$(function() {
$( "#driver_uuidHOS" ).combobox();
});
</script>
我已经声明了两个变量: -
<script src="js/new/jquery-1.8.3.js"></script>
<script>
var jq13 = jQuery.noConflict(true);
</script>
<script src="js/new/jquery-ui.js"></script>
<script>
var jq131 = jQuery.noConflict(true);
</script>
所以请告诉我我必须在哪里使用JQ13以及我的代码中的JQ131。此代码适用于可搜索的AutoSuggest。提前致谢。
加载第一个版本后,您可以将其分配给变量。
var jq13 = jQuery.noConflict(true); // as you did
,然后加载您的第二个jQuery版本。可以使用JQ13(...)访问您加载的第一个,而第二个可以使用$(...)。
访问第二个。不需要在第二个脚本...
您想加载一个版本的jQuery,然后是该版本的所有插件,然后打开没有冲突模式。您可以根据需要重复此过程多次(对于每个版本的jQuery Core需要加载一次)。
因此,对于您的特定情况,您想做的是:
<script src="js/new/jquery-1.8.3.js"></script>
<script src="js/new/jquery-ui.js"></script>
<script src="path/to/your/code.js"></script>
<script>jQuery.noConflict(true);</script>
path/to/your/code.js
是指您上面定义Combobox小部件的代码块。在支持多个jQuery版本方面,是否像您的示例一样包含页面,或像我的示例一样作为外部文件。您拥有的匿名功能包装器(以及所有插件都应该具有),该包装存储对jQuery
的引用是使此工作的原因。在执行这些文件时,jQuery
指向正确的版本,这些文件将其本地引用存储为$
。稍后,当您可以jQuery.noConflict(true)
时,即使全局jQuery
变量已更改,这些文件仍将继续保存其参考。如果您遵循此模式,即在加载所有其他文件之后均无冲突模式,则无需将jQuery分配给新变量,因为您的所有代码已经具有必要的参考。这种方法的好处是,您永远不会在包装器功能中获得可变的名称,并且可以保证可以与该模型开发的所有现有插件一起使用,这都是所有官方的jQuery项目,并且可能具有任何具有的插件考虑没有冲突支持。
最后一件事:在脚本的底部,您将jQuery称为$
,但是您不在存储$
参考的功能包装器中。如果这实际上是您的应用程序中的两个单独的文件,请确保将包装器包含在每个文件中。如果这实际上是上面所示的单个脚本,请确保将文档在包装器内部移动。