第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何通過 JS 更改 CSS 類樣式

如何通過 JS 更改 CSS 類樣式

胡子哥哥 2022-09-29 15:18:14
我有一個像這樣的css:.title-bottom:before {    bottom: -5px;    content: "";    height: 2px;    left: 0;    position: absolute;    width: 80px;}左:0將下劃線設(shè)置為左側(cè),但當 RTL 處于活動狀態(tài)時,它必須向右浮動。所以,我想向左改變:0向左:初始,如果 rtl 存在。我該怎么做?我開始這樣編寫代碼:if (document.dir === 'rtl'){但我無法繼續(xù)。因為我找不到學習JS的好資源。我需要一個代碼來解決這個問題,也是學習JS的好資源。
查看完整描述

1 回答

?
元芳怎么了

TA貢獻1798條經(jīng)驗 獲得超7個贊

您正在尋求更改整個文檔的 CSS 規(guī)則。


執(zhí)行此操作的一種方法是將樣式表附加到文檔的 中,然后將規(guī)則更改放在那里。由于添加的樣式表是文檔中的最后一個樣式表,因此它將覆蓋默認規(guī)則。<head>


if (document.dir === 'rtl') {


  // create a new style sheet and append to head

  let newStyle = document.createElement('style');

  newStyle.innerHTML = '.title-bottom:before { left:initial; }';

  document.head.appendChild(newStyle);

}

function addRtl() {

  let newStyle = document.createElement('style');

  newStyle.innerHTML = '.title-bottom:before { content: ".rtl"; }';

  document.head.appendChild(newStyle);

  document.dir = 'rtl';

}

.title-bottom {

  background-color: lightgray;

  padding: 1rem;

}

<body>

<h1>RTL Style Manipulation</h1>

<button onclick="addRtl()">Add New Style Sheet</button>

<div class="title-bottom">.title-bottom</div>

</body>

替代方法:CSS 屬性


但是,由于您基于名為“dir”的屬性進行更改,因此您不需要任何JavaScript來實現(xiàn)此目的。相反,您可以使用 CSS [屬性 = 值] 選擇器。


CSS 屬性選擇器具有窗體 ,它將匹配將該屬性設(shè)置為該值的元素。[attribute=value]


要在 進行樣式修改時,可以使用:document.dir === 'rtl'


[dir*=rtl] .title-bottom:before {

  left:initial;

}

顯示如何使用 CSS 屬性選擇器的小示例:


function rtl() {

  document.dir = 'rtl';

}


function ltr() {

  document.dir = 'ltr';

}

[dir=rtl] p {

  color: red;

}

<h1>Change document.dir</h1>


<button onclick="rtl()">set rtl</button>

<button onclick="ltr()">set ltr</button>

<p>Paragraph text will be red when document.dir === 'rtl'</p>


查看完整回答
反對 回復 2022-09-29
  • 1 回答
  • 0 關(guān)注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號