我對jQuery相當陌生,但設法開發了一個腳本,用於在特定的斷點處操縱th和td標籤的高度。它基本上查找給定表格的每一行中的最高th或td,並將該高度應用於該特定行中的所有th和td標籤,然後移動到下一行等。如何使jQuery腳本適用於多個html表格
該腳本是必需的,因爲一個錯誤的Wordpress插件,它錯誤地改變了特定斷點處表的外觀。該插件是我們開發的核心,無法更改,所以我們必須使用jQuery來操縱表格的外觀。
一切工作正常,但現在我需要它爲任何給定頁面上顯示的多個表工作。
每個表都有類「tablepress」,我已經試過周圍,我們的腳本與
$('table.tablepress').each(function() {
...our script...
})
,但不工作和邏輯是打破我的心:)
這裏是我們的腳本:
jQuery(document).ready(function($){
$(window).resize(function(){
//get breakpoint as defined in table classes (desktop,tablet,phone)
var responsivemode = $("#tablepress-999-no-2").attr('class').split(/ |-/);
var breakpoint = 0;
if($.inArray('desktop',responsivemode) > -1){
var breakpoint = 1200;
}else if($.inArray('tablet',responsivemode) > -1){
var breakpoint = 980;
}else if($.inArray('phone',responsivemode) > -1){
var breakpoint = 768;
}else{
var breakpoint = 0;
}
//only manipulate table if breakpoint reached
if (parseInt($(window).width()) < breakpoint) {
var myobject = {};
//1.each tr has the same number of ths/tds; each th/td has the same class to identify its position in the row - <th class="column-1"><th class="column-2">...<td class="column-1"><td class="column-2">
//2.loop through each thead row, getting th class and height
//3.check if class already stored in myobject; if yes, check if current th height in loop is greater than value in myobject and overwrite it; if class not yet stored in myobject, add it
//4.loop
$("#tablepress-999-no-2 thead tr").each(function(){
$(this).find('th').each(function(){
var currentthclass = $(this).attr('class').split(' ')[0];
var currentthheight = $(this).height();
if(myobject.hasOwnProperty(currentthclass)){
if($(this).height() > myobject[currentthclass]){
myobject[currentthclass] = currentthheight;
}
}else{
myobject[currentthclass] = currentthheight;
}
});//end th loop
});//end tr loop
//1.loop through each tbody row, getting td class and height
//2.check if class already stored in myobject; if yes, check if current td height in loop is greater than value in myobject and overwrite it; if class not yet stored in myobject, add it
//3.loop
$("#tablepress-999-no-2 tbody tr").each(function(){
$(this).find('td').each(function(){
var currenttdclass = $(this).attr('class').split(' ')[0];
var currenttdheight = $(this).height();
if(myobject.hasOwnProperty(currenttdclass)){
if($(this).height() > myobject[currenttdclass]){
myobject[currenttdclass] = currenttdheight;
}
}else{
myobject[currenttdclass] = currenttdheight;
}
});//end td loop
});//end tr loop
//1.loop through myobject getting class name and height
//2.apply new heights to all th and td tags in table
$.each(myobject, function(keyobj,valueobj){
$('#tablepress-999-no-2 tbody tr td.'+keyobj).each(function(){
$(this).height(valueobj);
});
$('#tablepress-999-no-2 thead th.'+keyobj).each(function(){
$(this).height(valueobj);
});
});
}else{
//if current window size not below breakpoint, return all th and td heights to original size;
$('#tablepress-999-no-2 tbody td').css('height','auto');
$('#tablepress-999-no-2 thead th').css('height','auto');
}//end check breakpoint
})//end resize function
});
在此先感謝您的幫助!
謝謝,但是當每個表的ths和tds具有相同的類名稱時,我最後一個表的高度調整也會應用到第一個表中?我猜我不知何故需要在腳本應用到下一個表之前清空myobject ..? –
@RichardTinkler我更新了答案。試試吧,讓我知道 – adiga
謝謝。真的很接近!最後一個問題是:$ .each(myobject ...)循環中的this關鍵字返回myobject而不是當前表。 –