最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 JavaScript 中動(dòng)態(tài)設(shè)置某些標(biāo)簽的樣式

在 JavaScript 中動(dòng)態(tài)設(shè)置某些標(biāo)簽的樣式

喵喔喔 2023-12-04 19:23:48
例如,要設(shè)置正文的樣式,您可以簡單地執(zhí)行document.body.style.color = "white"此操作,但如果我想使用 -tag 執(zhí)行相同的操作,pre我該怎么做?
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

最好的解決方案是通過類來改變樣式。這通常就是主題的工作方式。你在身體上設(shè)置了一個(gè)類來改變你想要改變的東西。


window.setTimeout( function () {

  document.body.classList.add("luckyGreen")

}, 4000)


window.setTimeout( function () {

  document.body.classList.remove("luckyGreen")

}, 8000)

pre {

  background-color: #CCC;

}



body.luckyGreen {

  color:green;

}

body.luckyGreen pre {

  background-color: #CFC;

}

<pre>

 Hello there

</pre>

X

<pre>

 Hello there

</pre>

Y

<pre>

 Hello there

</pre>

但由于某種原因你想改變一個(gè)類,你可以編寫一個(gè)新的 css 規(guī)則。


function addRule() {

  var styleEl = document.createElement('style');

  document.head.appendChild(styleEl);

  var styleSheet = styleEl.sheet;

  var ruleStr = "pre { background-color: red; }"

  styleSheet.insertRule(ruleStr, styleSheet.cssRules.length);

}



window.setTimeout(addRule, 4000)

<pre>

 Hello there

</pre>

X

<pre>

 Hello there

</pre>

Y

<pre>

 Hello there

</pre>


查看完整回答
反對(duì) 回復(fù) 2023-12-04
?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊

在這種情況下,您需要使用getElementsByTagName如下:


var tags = document.getElementsByTagName("pre");

for(var i = 0; i < tags.length; i++)

     tags[i].style.color = "white";


查看完整回答
反對(duì) 回復(fù) 2023-12-04
?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以在 CSS 中創(chuàng)建類,然后切換它們或添加/刪除它們或手動(dòng)執(zhí)行類似的操作。


let v3 = document.getElementsByTagName("pre")[2];

v3.classList.add('three');


let pre = document.getElementsByTagName("pre")[0];

pre.classList.add('one');


let preAll = document.getElementsByTagName("pre");

preAll[3].classList.add('four');


let i;

for (i = 0; i < preAll.length -1; i++) {

  preAll[i].style.backgroundColor = "#ddddff";

}

pre.one {

  color: red;

  font-weight: bold;

}


pre.two {

  color: blue;

  font-weight: 2;

}


pre.three {

  background-color: #ddffdd;

}


pre.four {

  color: green;

  background-color:#ffddff;

  font-weight: bold;

}

<div class="container">

  <pre>pretty pre</pre>

  <pre>pretty pre</pre>

  <pre>pretty pre</pre>

  <pre>pretty pre</pre>

</div>


查看完整回答
反對(duì) 回復(fù) 2023-12-04
  • 3 回答
  • 0 關(guān)注
  • 266 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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