我已经使用 JQuery 生成了一个 HTML 表格,并希望在文章中使用一些 span 元素来填充表格。
该表在两行上生成两种信息类型,但我需要它在同一行上作为记录的一部分。
在过去的几个小时里,我尝试了一百件事,但仍然无法解决问题。我必须保持标签打开以允许第二个跨度进入第二列,但我必须以这种方式使用两个循环,另一种方式我有点接近实现我的目标如下:
jQuery
$(document).ready(function()
{
$('#blockbuster_top_100_films').hide();
$('#try_this').click(function()
{
$('#content').append('<table border="1"/>');
$('table').prepend('<th> Movies </th>', '<th> Description </th>');
$('span[class="title"], span[class="description"]').each(function()
{
$('table').append('<tr><td>' + $(this).text() + '</td><td>' + $(this).text() + '</td></tr>');
});
});
});
HTML
<body>
<input type='button' value='Try this' id='try_this' />
<section id="blockbuster_top_100_films">
<article>
<span class="title">Cowboys & Aliens</span>
<span class="link">http://www.blockbuster.com:80/catalog/movieDetails/363773</span>
<span class="description">Based on the graphic novel by Scott Mitchell Rosenberg, Cowboys & Aliens is set in 1800s Arizona, where the local cowboys, headed by gunslinger Jake Lonergan (Daniel Craig), and the indigenous Apache...</span>
<img src="http://images.blockbuster.com/is/amg/dvd/cov150/drv200/v286/v28605bhsao.jpg?wid=65&&hei=91&cvt=jpeg" />
</article>
<article>
<span class="title">The Change-Up</span>
<span class="link">http://www.blockbuster.com:80/catalog/movieDetails/483305</span>
<span class="description">A married father and a swinging single swap bodies after a wild night of drinking, and do their best not to throw each other's lives into complete chaos while scrambling to figure out a way to get...</span>
<img src="http://images.blockbuster.com/is/amg/dvd/cov150/drv200/v273/v27351ldlup.jpg?wid=65&&hei=91&cvt=jpeg" />
</article>
<article>
<span class="title">Super 8</span>
<span class="link">http://www.blockbuster.com:80/catalog/movieDetails/486208</span>
<span class="description">Writer/director J.J. Abrams teams with producer Steven Spielberg for this period sci-fi thriller set in the late '70s, and centering on a mysterious train crash in a small Ohio town. Summer, 1979: a...</span>
<img src="http://images.blockbuster.com/is/amg/dvd/cov150/drv200/v281/v28148xntyb.jpg?wid=65&&hei=91&cvt=jpeg" />
</article>
<article>
<span class="title">The Hangover Part II</span>
<span class="link">http://www.blockbuster.com:80/catalog/movieDetails/453672</span>
<span class="description">A modest bachelor brunch devolves into a wild weekend in Bangkok when the gang travels to Thailand to see Stu get married. Still traumatized by memories of the Las Vegas fiasco, Stu (Ed Helms) vows...</span>
<img src="http://images.blockbuster.com/is/amg/dvd/cov150/drv200/v275/v27553l0jmg.jpg?wid=65&&hei=91&cvt=jpeg" />
</article>
</section>
</body>