I have a code like so:
<body style='opacity:0' onload='this.style.opacity=1'>
I would like to change opacity of body once everything is loaded. Is there a reason this wont work. Can I change this opacity? If I reference by the tag body it will work. For example using jquery :
<body style='opacity:0' onload='$("body")[0].style.opacity=1'>
Will work but :
<body style='opacity:0' onload='$(this)[0].style.opacity=1'>
Will not work. Can I use this somehow?
Is there a reason 'this' wont work.
this has no context inside the onload that why it's not working.
If I reference by the tag 'body' it will work
$("body")[0] refer to the body that why it's work, you could use document.body also.
When I
onload='alert(this)'why do I get anobject?
Since this keyword in global scope refers to window object.
Because you're using jQuery you could avoid the inline events and use ready function instead of onload and change the style using css() :
//When the page is fully loaded
$(function(){
//Change the opacity to 1
$("body").css('opacity','1');
});
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With