1 回答
![?](http://img1.sycdn.imooc.com/545863c10001865402200220-100-100.jpg)
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>
添加回答
举报