简体   繁体   中英

Setting style attributes to an element added via Write()

I want the user to see a blueish button with a large text on it, after he presses on an image. I wrote this code in javascript using the Write() method:

<script>
function DoThis()
{
s="<button style=background-color:#aaccdd font-size:50px >Hello</button>";
document.write(s);
}
</script>

What I see is only a blue button, but the font size is not changing. I tried to add coma, semicolons etc' but non of it worked. Any Ideas?

You need quote for the style tag and semicolon separator:

s="<button style=\"background-color:#aaccdd;font-size:50px;\" >Hello</button>";

Edit : The \\ before the semicolon is here to say that the next character in the code (the semicolon) should be considered as part of the current string.

You forgot to wrap style value in ' or ", try this:

<script>
function DoThis()
{
s='<button style="background-color:#aaccdd; font-size:50px;" >Hello</button>';
document.write(s);
}
</script>

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