我知道还有很多其他问题涉及这个主题,但没有一个专门解决我的问题。
我正在获取一个 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 文件中的一些标签,我怎么能正确地将它作为使用 Jquery<item>
写入表中?<a href="...>
非常感谢您的帮助!