Advanced jQuery Chaining, Data Attributes, Selector Optimization, and Modern Front‑End CSS Practices
This article explains how to use jQuery’s chaining and .end() method, correctly employ HTML5 data‑* attributes, improve selector performance by caching and preferring native loops, understand the CSS box model and box‑sizing, apply efficient float and absolute positioning techniques, and follow best‑practice CSS formatting and feature detection.
jQuery chaining and the .end() method allow returning to previous selections, enabling complex DOM traversals.
$(".quote")
.hide()
.find("a").text("Click here").bind("click",doStuff).end()
.parent().removeClass().addClass("testimonial").draggable().end()
.fadeIn("slow");HTML5 data-* attributes can store typed data; jQuery .data() converts string values to proper JavaScript types, but attribute names must be hyphen‑separated and lowercase.
Using .data() returns correct types:
typeof $("#test").data("isBool"); // boolean
typeof $("#test").data("someNumber"); // numberSelector performance can be improved by caching jQuery objects and using efficient selectors; native for loops are faster than $.each() .
// before
$(".quote a").bind("click", doStuff);
// now
var $quoteLinks = $(".quote a");
$quoteLinks.bind("click", doStuff);
$quoteLinks.addClass("quoteLink");
$quoteLinks.fadeIn("slow");CSS box‑model differences between quirks and standards mode are explained, and the box-sizing property is recommended to make width calculations intuitive.
.foo {
width: 200px;
padding: 25px;
border: 25px solid;
margin: 20px;
box-sizing: border-box;
}Float‑based layouts require a containing element, while absolute positioning with a relative parent gives precise control; examples show both approaches.
.parent { position: relative; width: 310px; height: 210px; }
.one, .two, .three { position: absolute; }
.one { top:0; left:0; width:200px; height:210px; }
.two { top:0; right:0; width:100px; height:100px; }
.three { bottom:0; right:0; width:100px; height:100px; }Best‑practice CSS formatting tips include one‑line vs multi‑line declarations, grouping related properties, avoiding unnecessary !important , and using clearfix via overflow:auto or modern micro‑clearfix techniques.
Feature detection should rely on libraries like Modernizr rather than browser sniffing, and @font-face usage requires attention to licensing and proper fallback strategies.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.