我正在嘗試定位使用偽元素::before和放置的排序圖標::after。這看起來像:jQuery(function($){ $('#fpTable').on( 'draw.dt', function (e) { $('#fpTable thead tr th:visible:not(".no-sort")').each(function(idx, ele) { let leftPos = Math.round(($(ele).width() + $(ele).textWidth()) / 2); let rightPos = leftPos+8; $(ele).attr('id','fp_sort_col_'+idx); $('#fp_sort_col_'+idx).append("<style>::before{left:"+leftPos+"px !important;right: auto !important;}::after{left:"+rightPos+"px !important;right: auto !important;}</style>"); }) }); $('#fpTable').dataTable({ "bLengthChange" : false, "paging": false, "bFilter": false, "columnDefs": [ { "targets" : 'no-sort', "orderable": false, }], // "stateSave": true, // "order": [[ 6, "asc" ]] }); }); $.fn.textWidth = function(){ var html_org = $(this).html(); var html_calc = '<span>' + html_org + '</span>'; $(this).html(html_calc); var width = $(this).find('span:first').width(); $(this).html(html_org); return width; };在這里,我在第 1、4 和 6 列有排序圖標。我想將這些圖標放在文本之后th。而且,為此,我計算了每個 th 的寬度,并嘗試使用絕對位置在其后放置圖標。為此,我已經添加id到每個th需要更新位置的地方。然后,嘗試將樣式附加到這些 id,但發生的情況是,它沒有將 CSS 應用于#id::before,而是應用于th::before. 因此,最后一列的位置值也在更新第一列和第四列的位置值。如果你需要jsfiddle鏈接。圖片是為了讓事情更清楚。#PS 寬度可能因屏幕尺寸而異。
通過 jquery 添加偽元素 CSS 到特定的 id 也被應用于其他 th 元素
蝴蝶刀刀
2023-06-15 10:00:49