Working example: http://jsfiddle.net/JVVcA/
HTML:
<fieldset id="data-page"> <legend>data-page</legend> <button rel="page1">Highlight page one</button> <button rel="page2">Highlight page two</button> <div data-page="page1"> <h1 id="page1">Page one</h1> <h1 id="page2">Page two</h1> </div> </fieldset> <fieldset id="class"> <legend>class</legend> <button rel="page3">Highlight page three</button> <button rel="page4">Highlight page four</button> <div class="page3"> <h1 id="page3">Page three</h1> <h1 id="page4">Page four</h1> </div> </fieldset>
CSS
fieldset { border: 1px solid #aaa; padding: 5px; } h1 { background-color: white; } div[data-page="page1"] h1#page1 { background-color: pink; } div[data-page="page2"] h1#page2 { background-color: pink; } div.page3 h1#page3 { background-color: cyan; } div.page4 h1#page4 { background-color: cyan; }
JS:
$('#data-page button').click(function(){ var rel = $(this).attr('rel'); $(this).siblings("div").attr('data-page', rel); }); $('#class button').click(function(){ var rel = $(this).attr('rel'); $(this).siblings("div").attr('class', rel); });
Initial load:

After clicking the โSelect Page Twoโ and โSelect Page Fourโ buttons in Webkit (in particular, in Google Chrome for Windows 7):

After that in Firefox:

As you can see, the data-page selector works fine when the data-page is initially rendered, but when the DOM is controlled on the fly, the styles defined by the CSS [data-page="???"] selector do not affect accordingly. Compare this to the situation with class selectors. When classes change on the fly, styles change as expected.
Perhaps this is due to the fact that I encountered situations when using this attribute selector in combination with CSS transitions in which a similar lack of reaction occurs, but in these cases, clicking elsewhere on the page, waving the mouse or just waiting for a bit in the end the result leads to the expected change.
So, is there a way around this otherwise than just raising your hands and not using data-page style attributes?
source share