简体   繁体   中英

How to write \9 IE8 hack inside Javascript

When using normal CSS stylesheet I just use such code:

display:inline;
display:block\9;

and it works instantly.

But when I got the same code inside JavaScript it just doesn't work making whole input field invisible. How the \\9 hack for IE8 should be entered ?

inp.style.display="inline";
inp.style.display="block\9";

您需要转义反斜杠,因为它是字符串中的特殊字符:

inp.style.display = 'block\\9';

My guess is that JS is interpreting the "\\9" as a literal "9" and you are really ending up with inp.style.display="block9"; .

To fix this, use a double backslash:

inp.style.display="block\\\\9";

Not tested, but it's something to try.

Some context about the \\9 CSS hack.

I'd advise you against using hacks for IE9 as this is the last version of IE considering conditional comments.
Conditional classes are a better practice imho, more robust or future-proof.

Otherwise Browserhacks is a great resource when you've to use one those dreaded hacks. Filter for IE 9 and you'll have hacks using JS properties for IE6-10 or 8-10 or 9 only, etc.
The CSS hacks proved reliable when I had to use them (sigh), I didn't test the JS ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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