亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 jquery 添加偽元素 CSS 到特定的 id 也被應用于其他 th 元素

通過 jquery 添加偽元素 CSS 到特定的 id 也被應用于其他 th 元素

蝴蝶刀刀 2023-06-15 10:00:49
我正在嘗試定位使用偽元素::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 寬度可能因屏幕尺寸而異。
查看完整描述

1 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

您要附加以下內容:


<style>::before{le ....

這將選擇所有元素::before并向 DOM 中存在的每個可以具有::before偽元素的元素添加一個偽元素。你可以做的是:


$("#fp_sort_col_" + idx).append(

  "<style>#fp_sort_col_" + idx + "::before{left:" +

    leftPos +

    "px !important;right: auto !important;}#fp_sort_col_" + idx + "::after{left:" +

    rightPos +

    "px !important;right: auto !important;}</style>"

);


查看完整回答
反對 回復 2023-06-15
  • 1 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號