如何使用jQuery将HTML表转换为?

如何使用jQuery将HTML表转换为?,jquery,html,Jquery,Html,我如何将下面提供的HTML表转换为元素?长话短说,我正在使用一个PHP脚本,将内容输出到一个HTML表中,事实证明,编辑PHP并将表元素转换为的非常困难。以下是HTML表格代码: <table><tbody><tr data-marker="0"><td class="mapp-marker"><img class="mapp-icon" src="http://www.walmartchicago.com/wp-content/plugin

我如何将下面提供的HTML表转换为元素?长话短说,我正在使用一个PHP脚本,将内容输出到一个HTML表中,事实证明,编辑PHP并将表元素转换为的非常困难。以下是HTML表格代码:

<table><tbody><tr data-marker="0"><td class="mapp-marker"><img class="mapp-icon" src="http://www.walmartchicago.com/wp-content/plugins/mappress-google-maps-for-wordpress/icons/walmart-yellow.png"></td><td><b>Walmart Neighborhood Market<br></b><a href="#" class="poi_list_directions">Get Directions</a></td></tr><tr data-marker="1"><td class="mapp-marker"><img class="mapp-icon" src="http://www.walmartchicago.com/wp-content/plugins/mappress-google-maps-for-wordpress/icons/walmart-red.png"></td><td><b>Walmart Express<br></b><a href="#" class="poi_list_directions">Get Directions</a></td></tr><tr data-marker="2"><td class="mapp-marker"><img class="mapp-icon" src="http://www.walmartchicago.com/wp-content/plugins/mappress-google-maps-for-wordpress/icons/walmart-red.png"></td><td><b>Walmart Express<br></b><a href="#" class="poi_list_directions">Get Directions</a></td></tr><tr data-marker="3"><td class="mapp-marker"><img class="mapp-icon" src="http://www.walmartchicago.com/wp-content/plugins/mappress-google-maps-for-wordpress/icons/walmart-blue.png"></td><td><b>Walmart Supercenter<br></b><a href="#" class="poi_list_directions">Get Directions</a></td></tr><tr data-marker="4"><td class="mapp-marker"><img class="mapp-icon" src="http://www.walmartchicago.com/wp-content/plugins/mappress-google-maps-for-wordpress/icons/walmart-blue.png"></td><td><b>Walmart Supercenter<br></b><a href="#" class="poi_list_directions">Get Directions</a></td></tr></tbody></table>
长话短说,我正在使用一个PHP脚本,将内容输出到一个HTML表中,事实证明,编辑PHP并将表元素转换为的非常困难

不赞成将表格用于非表格内容。但是,如果您已经在一个表中提供了内容,并且它看起来很好,那么您不应该使用jQuery以div形式转换表,而只是为了摆脱表


如果您仍然想使用jQuery在DIV中打开表,则必须考虑元素的结构。然后,循环遍历每个表行,循环遍历每个表单元格,并将内容移动到新创建的表中。最后,用一组s替换表。

无需循环元素和复制属性,只需将表内容作为字符串拉出来,然后运行一个简单的字符串替换

$('table').replaceWith( $('table').html()
   .replace(/<tbody/gi, "<div id='table'")
   .replace(/<tr/gi, "<div")
   .replace(/<\/tr>/gi, "</div>")
   .replace(/<td/gi, "<span")
   .replace(/<\/td>/gi, "</span>")
   .replace(/<\/tbody/gi, "<\/div")
);

这看起来确实是一件疯狂的事情。我只是写了一个jQuery插件来实现这一点。使用旧的web应用程序时,我们不想真正深入研究后端代码,而且预算有限。所以,只需将转储到表中的数据转换为div,这样我就可以将它们浮动起来,设置样式,使数据看起来更好

系统正在呈现并非真正的“表格”数据的数据


如果有人对未来感兴趣。

尽管编辑PHP以在divs中生成内容可能很困难,但它很可能是这里使用的最佳方法。我的答案将为您提供您所要求的jQuery解决方案。也许最好的解决方案是像我刚才所做的那样,只在从PHP输出字符串之前执行?样式化的无序列表将是适合这种类型内容的容器。您可以使用CSS背景作为LI上的标记。基本上,将整个表作为字符串,替换为,使用。当页面中有多个表时,此方法将失败。只需将初始选择器$table'替换为所需的选择器$my_table即可。因此,您建议为10个表复制相同的代码?更好的方法是:`$'table'.eachfunction{var$this=$this;$this.replaceWith$this.html.replace…;};你离题了。他问我如何转换HTML表…,这就是我的回答。但是,是的,您的建议对于一次转换多个表非常有用。
$('table').replaceWith( $('table').html()
   .replace(/<tbody/gi, "<ul id='table'")
   .replace(/<tr/gi, "<li")
   .replace(/<\/tr>/gi, "</li>")
   .replace(/<td/gi, "<span")
   .replace(/<\/td>/gi, "</span>")
   .replace(/<\/tbody/gi, "<\/ul")
);
$table_str = //table string here
$UL_str = str_replace('<tbody', '<ul id="table"', $table_str);
$UL_str = str_replace('<tr', '<li', $UL_str);
$UL_str = str_replace('</tr', '</li', $UL_str);
$UL_str = str_replace('<td', '<span', $UL_str);
$UL_str = str_replace('</td', '</span', $UL_str);
$UL_str = str_replace('</tbody', '</ul"', $UL_str);