如何在使用 Jquery 动态生成的表的每三行附加 html 标签
有没有一种方法可以使用 Jquery 在每三个动态生成的表格单元格之后插入 '' 标签,以便我最终得到一个动态三列表格?
请原谅我缺乏知识,我实际上是在尝试编写我的第一个 jquery 脚本,所以我一无所知。我知道 php,并且我有一个表,其中有一个循环,该表使用每个标签内的信息动态创建 。换句话说,它在静态
标记内动态创建表格单元格。问题是它不断输出表而不将它们分成行,这给我留下了一堆列。我读过关于此的其他文章,但似乎没有一篇文章与我有完全相同的问题,而且我仍在努力理解如何编写自定义 Jquery 代码。
php 代码非常长,充满了许多 if 语句和其他函数,所以我不打算将其发布在这里,只是为了使其更简单,我制作了我想要做的事情的微型模型。
<table id="mytable" width="266" border="1" cellspacing="10" cellpadding="10">
<tr>
<?php
$x=0;
while (have_products($x)) {
echo '<td>' . somelongassfunction() . '</td>';
$x++;
if (fmod($x,3) == 0) {
echo '</tr><tr>';
continue;
}
if ($x==20){
echo '</tr>';
}
}
function somelongassfunction(){
return 'Hello';
}
function have_products($a){
return $a<=20;
}
?>
</table>
此代码循环并动态添加表格单元格,直到我给它的限制,这将代表我的数据库项目。每三行,它会添加一个 或仅添加一个
,具体取决于循环是否继续。这将创建一个 3 列的表。我无法将此代码应用于我的脚本,因为它是一个非常长且复杂的脚本,有很多 if 语句和函数。没有办法在不破坏代码或必须从头开始重写所有内容的情况下做到这一点。
无论如何,我可以使用 Jquery 动态附加 tr 标签吗?我将如何应用它?
Is there a way I can use Jquery to insert '' tags after every three dynamically generated table cells so that I end up with a dynamic three column table?
Please excuse my lack of knowledge, I'm literally trying to write my first jquery script ever, so I know absolutely nothing. I know php and I have a table that has a loop within it that is dynamically creating <td></td>
with the information inside each tag. In other words it is dynamically creating the table cells within a static <tr></tr>
tag. The problem is that it keeps outputing tables without breaking them up into rows which leaves me with a bunch of columns. I've read other articles on this but none seem to have the exact same problem as I do, and I am still struggling to understand how to write custom Jquery code.
The php code is very long and is full of numerous if statements and other functions so I'm not going to post it here but just to make it a little simpler, I made miniature mockup of what I'm trying to do.
<table id="mytable" width="266" border="1" cellspacing="10" cellpadding="10">
<tr>
<?php
$x=0;
while (have_products($x)) {
echo '<td>' . somelongassfunction() . '</td>';
$x++;
if (fmod($x,3) == 0) {
echo '</tr><tr>';
continue;
}
if ($x==20){
echo '</tr>';
}
}
function somelongassfunction(){
return 'Hello';
}
function have_products($a){
return $a<=20;
}
?>
</table>
This code loops and dynamically adds table cells up to the limit I give it which would represent my database items. Every three rows, it adds either a <tr></tr>
or just a </tr>
depending on whether the loop continues or not. This creates a 3 column table. I can't apply this code for my script because it is a very long and complex script that has a lot of if statements and functions. There is no way of doing it like this without breaking the code or having to rewrite everything from scratch all over again.
Is there anyway I can append the tr tags dynamically with Jquery and how would I go about to applying this to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 方法是循环遍历所有选项卡,并将它们添加到新创建的标签中,这些标签本身会添加到表的 html 中。粗略地:(
这只是提供一个想法,并不是作为正式的 JQ 答案。如果有人想对其进行编辑以使其完全正常工作,请随意)。
The jQuery approach would be to loop through all of the tabs, and add them to newly created tags, which themselves are added to the html of the table. Roughly:
( this is just to give an idea, not intended as a formal JQ answer. If anyone wants to edit it so it works fully, feel free ).
您可以使用选择器来选择每第三行:
但是,如果您尝试附加结束 /tr 标记,请尝试在 PHP 中使用可重置的计数器来执行此操作(此代码应该可以帮助您开始):
You can use a selector to select every third row:
However if you are trying to append end /tr tags, try doing it in PHP using a counter that resets itself (this code should get you started):