如何用jquery替换属性并设置值
旧 html
<div class="container">
<table>
<tr>
<td width="106" valign="top" style="background: red">
<p align="center">text</p>
</td>
</tr>
</table>
</div>
新 HTML
<div class="container">
<table>
<tr>
<td style="width:106px; background: red;">
<p style="text-align:center;">text</p>
</td>
</tr>
</table>
</div>
提前致谢
最佳答案
您可以简单地使用 getAttribute() 函数来检索属性的值。所以,我将使用<td width="106" valign="top" style="background: red">
举个例子:
var style = 'width:' + $('#container td').getAttribute('width') + ';' + $('#container td').getAttribute('style');
$('#container td').attr('style', style);
将导致<td style="width:106px; background: red;">
。但是,这是假设您的表格中只有一个表格单元格。如果您有多个单元格,则只需使用此概念将此方法应用于表中的每个单元格即可。
关于javascript - 将属性无效的 html5 替换为 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43108083/