我知道有很多其他问题涉及到这个主题,但没有一个具体解决我的问题。把Jquery转换成HTML表的麻烦
我正在接受一个XML文件,用Jquery解析并将其转换为HTML表格。在视觉上,它看起来不错;表看起来应该如此。但实际上,Jquery使用html做了一些奇怪的事情。
这里的XML:
<?xml version="1.0" encoding="us-ascii"?>
<wsmenu>
<cat id="snacks">
<item>
<desc>marta’s fresh tortillas & camp bread, pit master fat</desc>
<price>4</price>
<wood>m</wood>
</item>
<item>
<desc>smoked almonds, chile salt</desc>
<price>4</price>
<wood>h</wood>
</item>
<item>
<desc>smoked marinated olives</desc>
<price>4</price>
<wood>o</wood>
</item>
<item>
<desc>crispy potatoes, smoked garlic aioli</desc>
<price>6</price>
<wood>h</wood>
</item>
<item>
<desc>smoked artichokes, lemon and parmesan</desc>
<price>9</price>
<wood>h</wood>
</item>
<item>
<desc>smoked hummus, pit master fat camp bread & tortilla chips</desc>
<price>8</price>
<wood>m</wood>
</item>
<item>
<desc>smoked whitefish dip, fresh tortilla chips</desc>
<price>9</price>
<wood>o</wood>
</item>
</cat>
<cat id="salads">
<item>
<desc>smoked chicory, chicken chicharron, mustard-red wine vinaigrette, provolone</desc>
<price>8</price>
<wood>m</wood>
</item>
<item>
<desc>3 kale salad, celery greens, guanciale, smoked pepita, manchego</desc>
<price>8</price>
<wood>m</wood>
</item>
<item>
<desc>hickory braised spelt, smoked almond pesto & tomato</desc>
<price>8</price>
<wood>h</wood>
</item>
<item>
<desc>beet and fresh ricotta salad, smoked pistachios</desc>
<price>8</price>
<wood>m</wood>
</item>
</cat>
<cat id="sandwiches">
<item>
<desc>pulled pork, bbq, house cole slaw, mustard & pickles</desc>
<price>8</price>
<wood>o</wood>
</item>
<item>
<desc>chopped beef, house mustard & pickles</desc>
<price>8</price>
<wood>h</wood>
</item>
<item>
<desc>woodshed burger, chopped beef, todays sausage, smoked cheddar, traditional garnish</desc>
<price>11</price>
<wood>h</wood>
</item>
</cat>
<cat id="simple plates">
<item>
<desc>brisket stuffed piquillo peppers, bone broth & cotija</desc>
<price>9</price>
<wood>h</wood>
</item>
<item>
<desc>local acorn squash, tuaca, dark brown sugar, smoked pepper hollandaise</desc>
<price>8</price>
<wood>h</wood>
</item>
<item>
<desc>today’s sausage, house mustard & pickles</desc>
<price>8</price>
<wood>h</wood>
</item>
<item>
<desc>pea tendrils, smoked chilies, citrus & hickory butter</desc>
<price>8</price>
<wood>o</wood>
</item>
</cat>
<cat id="tacos">
<item>
<desc>brisket stuffed piquillo peppers, bone broth & cotija</desc>
<price>9</price>
<wood>m</wood>
</item>
<item>
<desc>local acorn squash, tuaca, dark brown sugar, smoked pepper hollandaise</desc>
<price>9</price>
<wood>m</wood>
</item>
<item>
<desc>today’s sausage, house mustard & pickles</desc>
<price>10</price>
<wood>h</wood>
</item>
<item>
<desc>pea tendrils, smoked chilies, citrus & hickory butter</desc>
<price>9</price>
<wood>m</wood>
</item>
</cat>
<cat id="traditional q">
<item>
<desc>beef ribs</desc>
<price>12/lb</price>
<wood>o</wood>
</item>
<item>
<desc>pork ribs</desc>
<price>12/lb</price>
<wood>p</wood>
</item>
<item>
<desc>smoked beef tenderloin</desc>
<sub>served cold</sub>
<price>11</price>
<wood>h</wood>
</item>
</cat>
<cat id="new q">
<item>
<desc>smoked red fish enpapillote, vegetables of the season</desc>
<price>19</price>
<wood>o</wood>
</item>
<item>
<desc>game bird, wood braised spelt & smoked artichokes</desc>
<price>21</price>
<wood>h</wood>
</item>
<item>
<desc>fancy mushrooms of the season, 4 chiles, polenta, vin cotta & hen egg</desc>
<price>18</price>
<wood>h</wood>
</item>
<item>
<desc>bowl of ramen noodles, bone broth, pulled pork, chiles, & quail egg</desc>
<price>15</price>
<wood>p</wood>
</item>
</cat>
<cat id="dining with friends">
<sub>please allow 30 minutes, feeds 4 people</sub>
<item>
<desc>16 hour smoked beef shin, fresh ricotta, chile, smoked oil borracho beans, 3 kale salad</desc>
<price>75</price>
<wood>h</wood>
</item>
<item>
<desc>open fire paella of mussels, clams, shrimp rabbit-rattlesnake sausage, game bird, fennel aioli</desc>
<price>75</price>
<wood>m</wood>
</item>
<item>
<desc>60 oz. bistecca alla fiorentina, crispy potatoes</desc>
<price>90</price>
<wood>m</wood>
</item>
</cat>
<cat id="sides">
<item>
<desc>fresh chips</desc>
<price>5</price>
<wood>m</wood>
</item>
<item>
<desc>smoked Loaded baked potato</desc>
<sub>add pork or beef $3</sub>
<price>5</price>
<wood>h</wood>
</item>
<item>
<desc>borracho beans</desc>
<price>5</price>
<wood>h</wood>
</item>
<item>
<desc>mexican corn</desc>
<price>4</price>
<wood>m</wood>
</item>
<item>
<desc>confit potato salad, mint, red pepper, crispy garlic</desc>
<price>4</price>
<wood>m</wood>
</item>
</cat>
</wsmenu>
这里是Jquery的:
function parseXML(xml){
$(xml).find("cat").each(function()
{
$(".menu").append("<table class='mnutbl'><tr><th>"+$(this).attr("id")+"</th></tr>");
$(this).find("item").each(function()
{
$(".menu").append("<tr class='mnutbl'><td>"+$(this).find("wood").text()+
"</td><td>"+$(this).find("desc").text()+
"</a></td><td>"+$(this).find("price").text()+
"</td></tr></table>")});
});
};
电流输出:
Jquery is writing <table> and <th> with attribute "id" for each <cat> tag, then separate <tr> for each <item> tag (these <tr>'s are NOT contained in the <table>), like so:
<table class="mnutbl">
<tr>
<th>Snacks</th>
</tr>
</table>
<tr>
<td>m</td>
<td>marta's fresh...</td>
<td>4</td>
</tr>
etc.......
问题的两个部分:
每个
<cat>
标签需要自己的<table>
和<item>
标签中的每个<cat>
都需要是表格行内的那个<table>
。我在做我的Jquery代码有问题吗?某些
<desc>
值需要链接到图像,但不是全部。如果我只将<imgurl>
添加到XML文件中的<item>
标记的某些中,那么我怎样才能使用Jquery将它正确地写入<a href="...>
?
非常感谢您的帮助!
当然,编写这些代码可能有更有效的方法,但上述应该是解决您的问题的最简单的更改。 – Blazemonger 2012-07-26 16:40:18
这就是为什么我爱堆栈溢出!非常感谢你和tucuxi!有时你只需要一双新鲜的眼睛! 有关我的问题的第二部分的任何想法? – Spartacus 2012-07-26 16:47:44
这是一个不同的问题,你没有它的示例代码。我建议将这个帖子作为一个单独的问题发布(当然,在你试图自己处理它之后)。 – Blazemonger 2012-07-26 16:49:43