我正在使用JQuery 1.4.2,並試圖構建一個實質上顯示錶中的選擇列表的插件。我正在使用Jquery UI css類來格式化選擇列表中顯示的容器,並使用DataTables插件來格式化表格,檢索數據和過濾表格。選擇列表在FireFox 3.6中完美工作,但在IE 7中,列表從不出現。我不確定這是否是由於定位 - 我似乎無法弄清楚問題所在。爲什麼我的JQuery插件能夠在Firefox中完美工作,但在IE中卻不能?
(function($) {
// Shell for the plugin code
$.fn.tablePicker = function(options) {
// Plugin code
var tbl = null;
var options= options = $.extend({},$.fn.tablePicker.defaults, options);
return this.each(function() {
// for each item in selector
tbl= $('#'+options.tblName);
$(tbl).wrap(options.container);
if(options.header != null){
var headerHtml= '<div align="center">' + options.header + '</div>';
$(this).find("#tp-container").prepend(headerHtml);
}
$(this).addClass("ui-hidden-on-load").addClass("ui-tablepicker");
$(this).addClass("ui-widget").addClass("ui-widget-content");
$(this).addClass("ui-helper-clearfix").addClass("ui-corner-all");
$(this).addClass("ui-helper-hidden-accessible");
$(this).css("position", options.position);
var offsetFromObject= null;
var offset= {top:null, left:null};
try {
if(options.forinput){
offsetFromObject= options.forinput;
}else if(options.forAnchor) {
offsetFromObject= options.forAnchor;
}else{
alert("Warning: Tablepicker plugin did not find any control to bind to.");
}
// Use the specified parameter first
if(options.top != null){
offset.top= options.top;
}else{
var t= $("#"+offsetFromObject).offset();
offset.top= t.top;
}
if(options.left != null){
offset.left= options.left;
}else{
var l= $("#"+offsetFromObject).offset();
offset.left= l.left;
}
$(this).offset(offset);
$(this).css("z-index", "1");
} catch (e) {
alert('Tablepicker problem' + e);
}
tbl= _setUpDataTable(tbl);
_performBindings(tbl, this);
});
function _setUpDataTable(tbl){
tbl= $(tbl).dataTable({
"aoColumns" : options.aoColumns,
"bFilter" : options.bFilter,
"bPaginate" : options.bPaginate,
"bLengthChange" : options.bLengthChange,
"bAutoWidth" : options.bAutoWidth,
"sScrollY" : options.sScrollY,
"sPaginationType" : options.sPaginationType,
"bProcessing" : options.bProcessing,
"sAjaxSource" : options.sAjaxSource
});
return tbl;
};
function _performBindings(dataTable, picker){
var tableName= options.tblName;
// Bind hide list to all inputs
var tableFilter= tableName + '_filter';
$('input, select').live('focus', function(event) {
if ($(event.target).parent().attr('id') != tableFilter) {
_hideList(picker); // Don't hide the list on the datatable filter input focus.
}
});
// Don't bind hide list to the field we want to show the list for
if(options.forinput != null){
var inputToBind=$('#'+options.forinput);
$(inputToBind).unbind('focus');
// Bind to the field to show the list on.
$(inputToBind).focus(function() {
if (!$(picker).is(':visible')) {
$(picker).slideToggle();
}
});
}
// Allow binding to an anchor
if(options.forAnchor != null){
$("#"+options.forAnchor).click(function() {
if (!$(picker).is(':visible')) {
$(picker).slideToggle();
}
});
}
// Bindings for mouse over on table
var tbl= $('#'+tableName);
$(tbl).find('tbody tr').live('mouseover mouseout', function(event) {
if (event.type == 'mouseover') {
$(this).addClass('hover');
} else {
$(this).removeClass('hover');
}
});
// handle the click event of the table
$(tbl).find('tbody tr').live('click', function(event, ui) {
var aData = dataTable.fnGetData(this);
if (aData != null) {
$.isFunction(options.onClick) && options.onClick.call(this, aData);
}
_hideList(picker);
dataTable.fnFilter(""); // clear the datatable filter on select.
$("#"+tableFilter).find("input").val("");
});
// Hide list on esc.
$(document).keyup(function(e) {
if (e.keyCode == 27) { // esc
_hideList(picker);
}
});
}
function _hideList(picker) {
if ($(picker).is(':visible')) {
$(picker).slideToggle();
}
}
}
$.fn.tablePicker.defaults = {
header : null,
container : '<div id="tp-container" class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all"/>',
position : 'absolute',
top : null,
left : null,
tblName : 'list_table',
forinput : null,
forAnchor : null,
aoColumns : null,
bFilter : true,
bPaginate : true,
bLengthChange : false,
bAutoWidth : true,
sScrollY : "200px",
sPaginationType : "full_numbers",
bProcessing : true,
sAjaxSource : null,
onClick : null
};
})(jQuery);
我調用插件這樣:
$("#engine-picker").tablePicker(
{forinput: "engineFamily",
header: "Pick an Engine Family from this list. Use Search to narrow list.",
onClick: function(data){
var id = data[0];
var family = data[1];
var vendor = data[2];
var year = data[3];
var source = data[4];
var usesOdometer= data[5];
$("#engineId").val(id);
$("#engineFamily").val(family);
$("#engineMfg").val(vendor);
$("#engineYear").val(year);
$("#odometer").val(usesOdometer);
},
aoColumns: [ {"bVisible" : false}, null, null, null, null, {"bVisible" : false}],
sAjaxSource: './enginelist-data.do',
top: 296,
left:602
});
CSS類:
.ui-hidden-on-load{display: none;}
.ui-tablepicker { width: 35em; padding: .25em .25em 0; z-index: 1}
.ui-tablepicker .ui-tablepicker-header { position:relative; padding:.2em 0; }
.ui-widget-header div{ width: 100%}
預先感謝任何/所有答覆!
好的,所以選擇列表表格不會出現在頁面上,因爲不知何故div的top屬性被設置爲負值。
Z-INDEX: 1; LEFT: 404px; POSITION: absolute; TOP: -736px
即使當我通過代碼,我明確設置的頂部和左側通過偏移對象:
- offset {...} Object
top 296 Long
left 602 Long
有誰看到我在這裏失蹤?
感謝您的提示,我不知道顯影劑的IE7和IE8的工具加載項,因爲我總是依靠Firefox /螢火蟲。這幫助我找出了爲什麼div沒有出現,現在我需要找到原因。我明確地設置了$(this)的top:left屬性 - 在我的插件中,但似乎最重要的屬性被設置爲負數#儘管顯式調用。 $(this)不能是IE的正確上下文嗎? (見上文修改後的帖子) – Griff 2010-11-10 22:44:57