2011-12-13 34 views
1

我正在使用以下jQuery函數,其中我需要傳遞一個額外的參數來訪問作爲動態值的高度。有沒有辦法做到這一點?到目前爲止我的代碼是:jquery參數設置滾動高度動態

(需要得到該scrollHeight屬性的值)

(function (cash) { 
$.fn.Scrollable = function (options) { 
    var defaults = { 
     //ScrollHeight: 400, 
     //Height:height, 
     Width: 0 
    }; 

    var options = $.extend(defaults, options); 

    return this.each(function() { 
     var grid = $(this).get(0); 
     //var doc = this.document.getElementById("height").nodeValue.toString(); 

     var gridWidth = grid.offsetWidth; 

     var ScrollHeight = 400; //$(this).getElementById("height").Value; // height.toString(); 
     alert(""); 
     var headerCellWidths = new Array(); 
     for (var i = 0; i < grid.getElementsByTagName("TH").length; i++) { 
      headerCellWidths[i] = grid.getElementsByTagName("TH")[i].offsetWidth; 
     } 
     grid.parentNode.appendChild(document.createElement("div")); 
     var parentDiv = grid.parentNode; 

     var table = document.createElement("table"); 
     for (i = 0; i < grid.attributes.length; i++) { 
      if (grid.attributes[i].specified && grid.attributes[i].name != "id") { 
       table.setAttribute(grid.attributes[i].name, grid.attributes[i].value); 
      } 
     } 
     table.style.cssText = grid.style.cssText; 
     table.style.width = parseInt(gridWidth + 17) + "px"; 
     table.appendChild(document.createElement("tbody")); 
     table.getElementsByTagName("tbody")[0].appendChild(grid.getElementsByTagName("TR")[0]); 
     var cells = table.getElementsByTagName("TH"); 

     var gridRow = grid.getElementsByTagName("TR")[0]; 
     for (var i = 0; i < cells.length; i++) { 
      var width; 
      if (headerCellWidths[i] > gridRow.getElementsByTagName("TD")[i].offsetWidth) { 
       width = headerCellWidths[i]; 
      } 
      else { 

       width = gridRow.getElementsByTagName("TD")[i].offsetWidth; 
      } 

      cells[i].style.width = parseInt(width - 3) + "px"; 
      gridRow.getElementsByTagName("TD")[i].style.width = parseInt(width - 3) + "px"; 
     } 
     parentDiv.removeChild(grid); 

     var dummyHeader = document.createElement("div"); 
     dummyHeader.appendChild(table); 
     parentDiv.appendChild(dummyHeader); 
     //   if (options.Width > 0) { 
     //    gridWidth = options.Width; 
     //   } 
     var scrollableDiv = document.createElement("div"); 

     gridWidth = parseInt(gridWidth) + 17; 

     scrollableDiv.style.cssText = "overflow:auto;height:" + ScrollHeight + "px;width:" + gridWidth + "px"; 

     scrollableDiv.appendChild(grid); 
     parentDiv.appendChild(scrollableDiv); 
    }); 
};})(jQuery); 

我打電話如下功能:

$(document).ready(function() { 
    $('#<%=grdLog.ClientID %>').Scrollable(400); 
}).data("height", 400); ; 

回答

2

如果我理解你正確你想能夠將高度傳遞給函數調用。

變化:

var defaults = { 
    //ScrollHeight: 400, 
    //Height:height, 
    Width: 0 
}; 

要:

var defaults = { 
    //ScrollHeight: 400, 
    Height: 0, 
    Width: 0 
}; 

當你調用該函數,你現在可以設置這樣的高度:

$('#<%=grdLog.ClientID %>').Scrollable({ 
    Width : 400, 
    Height : 400 
}); 

然後你就可以訪問options.Heightoptions.Width任何地方在你的jQuery插件$.extend()方法調用。

+1

是的,它工作得很好。謝謝賈斯帕 – Anandhan