简体   繁体   English

Jquery/Javascript如何动态添加td(table)

[英]How Jquery/Javascript dynamically add td (table)

I need help, any ideas?我需要帮助,有什么想法吗?

Firstly i want to hide some input element when page open, My approach creating dropdown $counter (1-8) and then depending on $counter Example:3.首先,我想在页面打开时隐藏一些输入元素,我的方法是创建下拉菜单$counter (1-8) ,然后取决于$counter示例:3。

I create/show existing element dynamically ($counter)<td></td> inside tr #ID.我在tr #ID 中动态创建/显示现有元素($counter)<td></td>

How jQuery solve this problem? jQuery如何解决这个问题?

Can u provide me example of switch case in jQuery to show/hide with defined td #ID?您能否提供 jQuery 中的开关盒示例以显示/隐藏定义的td #ID?

Thanks Before谢谢之前

Rizq里兹克

To hide a cell row , you can do this:要隐藏单元格,您可以执行以下操作:

$("#your_td").closest("tr").hide();

That's becasuse it's more logical to hide all the row than a single cell.这是因为隐藏所有行比隐藏单个单元格更合乎逻辑。

Hope to have understood you.希望能理解你。

Suppose your input has id of "input-1", then you can hide it simply doing:假设您的输入的 id 为“input-1”,那么您可以简单地隐藏它:

// Assign a value to counter, might be some function to do that
var counter = '1';

// Get a reference to the element and hide it
var input = document.getElementById('input-' + counter);
input.style.display = 'none';

// or 

input.style.visibility = 'hidden';

and to show it again:并再次展示它:

input.style.display = ''; // empty string

// or 

input.style.visibility = 'visible';

The difference between the display and visibility properties is that the former will remove the element completely from the document flow, whereas the second will just make it invisible (read the relevant parts of the W3C CSS spec). displayvisibility属性之间的区别在于前者将从文档流中完全删除元素,而后者只会使其不可见(阅读 W3C CSS 规范的相关部分)。 Messing with display might make the document jump about a bit as it reflows, messing with visibility keeps it stable but leaves a blank space.弄乱显示可能会使文档在回流时稍微跳动,弄乱可见性使其保持稳定但会留下空白。 Your choice which suits best.您的选择最适合。

You can hide and show any kind of element using the same method.您可以使用相同的方法隐藏和显示任何类型的元素。 If you want the parent cell or row, go up the parentNode chain till you hit the first TD or TR as appropriate and hide it.如果您想要父单元格或行,go 在parentNode链上,直到您适当地点击第一个 TD 或 TR 并将其隐藏。

There is a bit of a tutorial on the Mozilla developer site for messing with table elements using the DOM APIs. Mozilla 开发者网站上有一些关于使用 DOM API 处理表格元素的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM