Javascript jQuery 选择器 -> 特定表中的所有 TD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4110480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:38:41  来源:igfitidea点击:

jQuery selectors -> all TD within a specific table

javascriptjquery

提问by sova

I am still learning jQuery and the selector bit is incredibly useful, but I still don't understand it perfectly well.

我仍在学习 jQuery,选择器位非常有用,但我仍然不太了解它。

I have a table with id=table1, and I want to select all td's in this table.
(really I want to wrap the text within each td with a div with overflow:hidden so I can force the cell heights to be uniform.)

我有一个 id=table1 的表,我想选择这个表中的所有 td。
(我真的想用溢出的 div 将每个 td 中的文本包裹起来:隐藏,这样我就可以强制单元格高度一致。)

What's the appropriate syntax for the jQuery(javaScript?) selector?

jQuery(javaScript?)选择器的合适语法是什么

Any links to awesome selector tutorials are also welcome.

也欢迎任何指向很棒的选择器教程的链接。

回答by Gabriele Petrioli

The following should do the trick

以下应该可以解决问题

$('#table1 td').wrapInner('<div class="no-overflow"></div>');

and add a css rule in your stylesheet

并在您的样式表中添加一个 css 规则

.no-overflow{
      overflow:hidden;
      /*and whatever other css properties here*/
 }

For completeness here is the documentation about

为了完整起见,这里是有关的文档

回答by Mike Robinson

This will select all the cells:

这将选择所有单元格:

$("#table1 td") 

jQuery uses CSS3 selectors, read about them here: http://api.jquery.com/category/selectors/

jQuery 使用 CSS3 选择器,请在此处阅读:http: //api.jquery.com/category/selectors/

回答by Onite

$("#table1").find("td");

回答by generalhenry

$("#table1 td").each(function() {
  var text = $(this).html();
  var div = $("<div class=hiddenOverflow></div>");
  div.html(text);
  $(this).html(div);
});