简体   繁体   English

我需要隐藏“tr”元素的CSS规则,其子元素“td”具有带有空值属性的子“输入”元素?

[英]What CSS rule do I need to hide “tr” elements whose children “td”'s have child “input” elements with empty value attributes?

I realize the question title is a bit confusing, but in reality, I mean to ask exactly what I've typed. 我意识到问题标题有点令人困惑,但实际上,我的意思是要问我输入的确切内容。

I am trying to write a CSS file for printing (nausea rising) and I am wanting to eliminate white space in unnecessary rows (the necessity, or rather, the lack thereof, is determined by input fields that have empty values). 我正在尝试编写一个用于打印的CSS文件(恶心上升),我想要消除不必要的行中的空白区域(必要性,或者更确切地说,缺少它,由具有空值的输入字段确定)。

The proper CSS syntax (if there exists any) is relentlessly eluding me. 正确的CSS语法(如果有的话)是无情地躲避我。

I have used selectors like this before: 我之前使用过这样的选择器:

input[type=text]

So I could assume (hopefully... gulp!) that I could use something like: 所以我可以假设(希望... gulp!)我可以使用类似的东西:

input[val='']

However: 然而:

1) I'm not sure using apostrophes/quotes is even legal inside those brackets (what's that area of a CSS selector called, again?) 1)我不确定在这些括号内使用撇号/引号是否合法(再次调用CSS选择器的那个区域是什么?)

2) I wouldn't know how to put a selector inside those brackets (if even possible) since the actual "tr"s are what I'm trying to target, just "tr"s with td children with input children with empty vals. 2)我不知道如何将一个选择器放在这些括号内(如果可能的话),因为实际的“tr”是我想要的目标,只是“tr”与td孩子的输入子句有空vals 。

Wallowing in random syntax, I tried: 在随机语法中,我试过:

tr[td input val=]

Very much NOT to my surprise, did I find that obviously this didn't work, then I realized I had no clue how to proceed, if I wanted to accomplish this strictly in CSS. 非常不令我惊讶的是,我发现显然这不起作用,然后我意识到我不知道如何继续,如果我想在CSS中严格完成这一点。

~ Takes a quick breathe 快速呼吸

Okay, I realize I'm asking a lot out of CSS, but if it's selector system wasn't so powerful I wouldn't even assume this possible. 好吧,我意识到我要求很多CSS,但如果它的选择器系统不那么强大,我甚至不会认为这是可能的。

Lastly, I am already implementing javascript, jQuery, and even server-side C#.net (WebMatrix) so if this answer is best solved using these other languages, I don't mind, I just want the simplest cleanest solution, if possible, as any scripting method I use will require the addition of another class to all td elements therein, and there are many, not to mention the scripting itself. 最后,我已经实现了javascript,jQuery,甚至服务器端的C#.net(WebMatrix),所以如果使用这些其他语言最好地解决这个问题,我不介意,我只想要最简单最干净的解决方案,如果可能的话,因为我使用的任何脚本方法都需要在其中的所有td元素中添加另一个类,并且有很多,更不用说脚本本身了。

Any help, as always, is greatly appreciated. 一如既往,任何帮助都非常感谢。

---------------------EDIT REGARDING ALNITAK'S ANSWER------------------------ ---------------------编辑关于ALNITAK的回答------------------------

I tried to add the following jQuery just before my window.print(); 我试着在window.print();之前添加以下jQuery window.print(); JavaScript command: JavaScript命令:

$('tr > td > input[value=""]').parent().parent().css('display', 'none');
$('tr > td > input[value=""]').parent().parent().css('visibility', 'hidden');

I have checked my # of parents, and all is as it should be there. 我检查了我的父母,所有的一切都应该在那里。 The only other thing that might be interfering is that I have razor embedded in the values by default (although they are explicitly assigned empty strings on page start so, it should be fine). 唯一可能干扰的是我在默认情况下将razor嵌入到值中(虽然它们在页面开始时显式分配了空字符串,所以应该没问题)。 Just in case though I also tried: 以防万一我也试过:

$('tr > td > input[value=null]').parent().parent().css('display', 'none');
$('tr > td > input[value=null]').parent().parent().css('visibility', 'hidden');

But, alas, to no avail. 但是,唉,无济于事。

If I'm understanding you correctly : 如果我正确理解你:

You can't do what you want to in CSS, not yet anyway. 你不能在CSS中做你想做的事,但不管怎样。

What you're looking for is a css parent selector. 您正在寻找的是一个css父选择器。

http://css-tricks.com/parent-selectors-in-css/ http://css-tricks.com/parent-selectors-in-css/

By using the selector: 通过使用选择器:

tr td input[value=''] 

You'll be selecting the inputs, not the <tr>s. 您将选择输入,而不是<tr>。

One way to achieve what you want is to have something "watch" the page you're on (from javascript) and apply a class to anything you want hidden, when you print. 实现目标的一种方法是让某些东西“监视”您正在使用的页面(来自javascript),并在打印时将类应用于您想要隐藏的任何内容。 Then in your print stylesheet just use that class to hide! 然后在您的打印样式表中使用该类隐藏!

You can't do this with pure selectors, because the end result of a selector is always the matching child node, not their ancestors. 您不能使用纯选择器执行此操作,因为选择器的最终结果始终是匹配的子节点,而不是它们的祖先。

You need to find those matching child nodes, and then climb back up the tree again, eg in jQuery: 您需要找到那些匹配的子节点,然后再次爬回树,例如在jQuery中:

$('tr > td > input[value=""]').parent().parent().css('color', 'red');

You can use .closest('tr') instead of .parent().parent() if you prefer. 如果您愿意,可以使用.closest('tr')代替.parent().parent()

Also using jQuery, you can use the .has() function to create a chain that matches elements containing particular children: 同样使用jQuery,您可以使用.has()函数创建一个匹配包含特定子元素的元素的链:

$('tr').has('> td > input[value=""]').css('color', 'red');

The .has() function is more efficient than using the :has pseudo-selector, although I would expect neither to be as efficient as the full selector above which takes advantage of document.querySelectorAll on modern browsers. .has()函数比使用:has伪选择器更有效,尽管我认为它既不像上面的完整选择器那样有效,它在现代浏览器上利用了document.querySelectorAll

If you can't get the input[value=""] part to work, try this: 如果您无法获得input[value=""]部分,请尝试以下操作:

$('tr > td > input').filter(function() {
    return this.value == '';
})....

Your prose translates to this CSS: 你的散文翻译成这个CSS:

!tr>td>input:matches(:not([value]),[value=''])

However, both ! 但是,两个! and :matches are CSS4 selectors that are not implemented yet. :matches是尚未实现的CSS4选择器 You'd be better off using JavaScript: 你最好使用JavaScript:

var qsa = document.querySelectorAll("tr>td>input"), l=qsa.length, i;
for( i=0; i<l; i++) {
    if( qsa[i].value == '') qsa[i].parentNode.parentNode.style.display = "none";
}

Try using the :has() selector: http://api.jquery.com/has-selector/ 尝试使用:has()选择器: http//api.jquery.com/has-selector/

Description: Selects elements which contain at least one element that matches the specified selector. 描述:选择包含至少一个与指定选择器匹配的元素的元素。

$('tr:has(td input[value=""])').css({color:'red'});

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

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