In spec-compliant browsers, tr[style=height:64px]
throws a DOM error, saying this is an invalid selector. The version with double quotes works. This is because the spec says that the value in an attribute selector must be a CSS identifier or a string. height:64px
is not an identifier, so it fails. Enclosing it in quotation marks (either single or double) makes it into a string, which works.
If you don't want to quote it, then you need to escape the colon using the escape mechanism for CSS identifiers. That is why [style=height\:64px]
works (second example below). For details, see this question, of which this question is essentially a duplicate.
[style="height\:64px"]
works because escaping the colon is essentially a no-op.
[style="height\3a 64px"]
works as expected, with or without surrounding quotes. \3a
is the CSS escape for colon, and the following space terminates the escape sequence. I don't know why this didn't work for you. If you were specifying this selector as a JavaScript string, perhaps you forgot to escape the backslash.
[style="height:64px"]
does not work because character references only have meaning in HTML. They don't have any meaning in CSS selectors.
None of this has anything to do with whether the style attribute value ends in a semicolon or not. So the description provided by the accepted answer is wrong. It only works because of the quotes.
Bottom line: just wrap your attribute values in quotes and stop worrying about it.
function test(sel) {
try {
console.log(sel, "yields", document.querySelector(sel));
} catch (e) {
console.log(sel, "fails with error", e.name);
}
}
test('tr[style=height:64px]');
test('tr[style=height\\:64px]');
test('tr[style="height:64px"]');
test('tr[style="height\\:64px"]');
test('tr[style="height\\3a 64px"]');
test('tr[style=height\\3a 64px]');
test('tr[style="height:64px"]');
<table>
<tr style="height:64px"></tr>
</table>