如何使用jQuery将表格td中的图片URL转换为img标签?
需求:将表格中含HTTP/HTTPS链接的TD内容转为IMG标签
我有一个包含文本、图片URL等内容的表格数据。若任意<td>元素中包含http/https链接,需将该链接作为src属性值包裹在<img>标签内。
示例代码
<table cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr> <td>1</td> <td>https://amp.dev/static/samples/img/product1_640x426.jpg</td> <td>Apple</td> <td>1.99</td> </tr> <tr> <td>2</td> <td>https://amp.dev/static/samples/img/product2_640x426.jpg</td> <td>Orange</td> <td>0.99</td> </tr> </tbody> </table>
期望输出
<table cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr> <td>1</td> <td><img src="https://amp.dev/static/samples/img/product1_640x426.jpg" /></td> <td>Apple</td> <td>1.99</td> </tr> <tr> <td>2</td> <td><img src="https://amp.dev/static/samples/img/product2_640x426.jpg" /></td> <td>Orange</td> <td>0.99</td> </tr> </tbody> </table>
内容的提问来源于stack exchange,提问作者Reddy
相关产品推荐
相关产品推荐

