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

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

獲取段落對齊值

獲取段落對齊值

冉冉說 2021-10-07 10:42:59
我想獲得段落對齊的值以放入 IF 條件。下面的示例失敗并且無法運行這是代碼const paragraph = document.getElementsByTagName('p');for (let i = 0; i < paragraph.length; i++) {    if (paragraph[i].getAttribute('align').value == 'center') {        console.log('paragraph' + i + 'centered')    }}<p align="center">this is a paragraph!</p>正如您所指出的,屬性中不存在 value 屬性。如何獲得align的值,無論是“center, left, right ..”?
查看完整描述

1 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

const paragraph = document.getElementsByTagName('p');

for (let i = 0; i < paragraph.length; i++) {

    if (paragraph[i].getAttribute('align') == 'center') {

        console.log('paragraph' + i + 'centered')

    }

}

<p align="center">this is a paragraph!</p>

但該align屬性已過時且已棄用。text-align即使閱讀樣式需要更多代碼,也最好使用。


const paragraph = document.getElementsByTagName('p');


for (let i = 0; i < paragraph.length; i++) {

  let computed = window.getComputedStyle(paragraph[i], null);

  let alignment = computed.getPropertyValue('text-align')

  if (alignment == 'center') {

    console.log('paragraph' + i + 'centered')

  }

}

p {

  text-align: center;

}

<p>this is a paragraph!</p>


查看完整回答
反對 回復 2021-10-07
  • 1 回答
  • 0 關注
  • 269 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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