[英]jQuery break out of table
I have a standard HTML formatted table, that dynamically generates the content, via Zend Framework. 我有一个标准的HTML格式表,通过Zend Framework动态生成内容。 From which I have an issue altering the form internally PHP side. 从中我有一个问题在PHP内部改变表单。 So I need to alter the tables appearance somehow. 所以我需要以某种方式改变表的外观。 With that on the occasion I have an element show up in one of the rows and when this element shows up I want to break out of the table and then do something after it then start the table again. 有了这个,我有一个元素出现在其中一行中,当这个元素出现时,我想要突破表,然后在它之后做一些事情,然后再次启动表。
Basically I want to inject the equivlant of 基本上我想注入相应的
</tbody></table>/*other stuff*/<table><tbody>
after the row containing the one element I seek which in this case is a label. </tbody></table>/*other stuff*/<table><tbody>
在包含我寻找的一个元素的行之后,在这种情况下是一个标签。
I tried $("label[for='theLable']").parents('tr').after('</tbody></table><br><table><tbody>')
which appears to ignore the ending table parts add the br, and then does a complete open/close tag for table and tbody within the same table I am trying to break out of so inbetween tr tags basically it adds this new table 我尝试了$("label[for='theLable']").parents('tr').after('</tbody></table><br><table><tbody>')
似乎忽略了结束表部分添加br,然后为同一个表中的table和tbody做一个完整的打开/关闭标记我试图突破所以inbetween tr标签基本上它添加了这个新表
Whats the best way to approach this concept? 什么是接近这个概念的最佳方式?
update with jsfiddle link 用jsfiddle链接更新
You can't really modify the HTML of the document the way you're thinking, since it's not a legitimate way to alter the DOM. 您无法以您的思维方式真正修改文档的HTML,因为它不是改变DOM的合法方式。
Instead, I would create a new table and .append
the rows you want to move to it, which will automatically move them from their current location (instead of copying them): 相反,我会创建一个新表并.append
你想要移动到它的行,它会自动从当前位置移动它们(而不是复制它们):
$(document).ready(function() {
var $trow = $('label[for="specialLabel"]').closest('tr'),
$table = $trow.closest('table');
$('<table>').append( $trow.nextAll().andSelf() ).insertAfter($table);
});
this approach won't work in js. 这种方法在js中不起作用。 What you could do if the table has not too many rows is this: 如果表没有太多行,你可以做什么:
var nextTable = $("table").after("/*other stuff*/<table id="nextTable"></table>");
//now you loop over the lines of your original table that you want to have after the "break", and move them to the nextTable:
var before = true;
$("table tr").each(function(){
if ($(this).has("[for='theLable']")) {
before = false;
}
if (!before) {
nextTable.append($(this));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.