I need to find all the elements of a block in a given node. Block elements are not only elements that have display:block in CSS, but also default block elements such as div and p .
I know that I can just get the computed style of the element and check the display property, however, my code will execute in a long loop, and the computed styles erase the reflow stack every time, so it will be very expansive.
I am looking for a trick to do this without getComputedStyle .
Edit
Here is my current code that I would like to improve:
var isBlockOrLineBreak = function(node) { if (!node) { return false; } var nodeType = node.nodeType; return nodeType == 1 && (!inlineDisplayRegex.test(getComputedStyleProperty(node, "display")) || node.tagName === "BR") || nodeType == 9 || nodeType == 11; };
Other editing
jQuery.css calls getComputedStyle under the hood. So not what I'm looking for.
My decision
Thanks everyone for the suggestions. Unfortunately, none of them matched what I was looking for. After repeatedly processing the documentation, I realized that there is no real way to do this without getComputedStyle. However, I came up with code that should avoid getComputedStyle as much as humanly possible. Here is the code:
$.extend($.expr[':'], { block: function(a) { var tagNames = { "ADDRESS": true,"BLOCKQUOTE": true,"CENTER": true,"DIR": true,"DIV": true, "DL": true,"FIELDSET": true,"FORM": true,"H1": true,"H2": true,"H3": true, "H4": true,"H5": true,"H6": true,"HR": true,"ISINDEX": true,"MENU": true, "NOFRAMES": true,"NOSCRIPT": true,"OL": true,"P": true,"PRE": true,"TABLE": true, "UL": true,"DD": true,"DT": true,"FRAMESET": true,"LI": true,"TBODY": true, "TD": true,"TFOOT": true,"TH": true,"THEAD": true,"TR": true }; return $(a).is(function() { if (tagNames[this.tagName.toUpperCase()]) { if (this.style.display === "block") { return true; } if (this.style.display !== "" || this.style.float !== "") { return false; } else { return $(this).css("display") === "block"; } } else { if (this.style.display === "block") { return } else { return $(this).css("display") === "block"; } } }); } });
Using this code is very simple, just do $ (": block") or $ ("form: block"). This will avoid the use of the .css property in many cases and only refuse it in a pinch.
Starx's answer gave me an idea to do this, so I'm going to mark his post as an answer.