最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set Div value before document ready - Stack Overflow

programmeradmin2浏览0评论

How to set Div value dynamically before document ready.

I have a div in my test page. I want to set the Div value dynamically before jquery document ready. Which event i can use.

Please help me.

Thanks

How to set Div value dynamically before document ready.

I have a div in my test page. I want to set the Div value dynamically before jquery document ready. Which event i can use.

Please help me.

Thanks

Share Improve this question asked Jul 8, 2011 at 6:01 pavanpavan 211 silver badge2 bronze badges 1
  • can you tell me what is your requirement , why do you want to set so early and not in css – kobe Commented Jul 8, 2011 at 6:05
Add a ment  | 

7 Answers 7

Reset to default 2

you can give it as inline code

it doesn't wait till the page load's , it will load as soon as it reaches that code.

<script language="javascript">

execute your code here

</script >

The only thing with this code is , the html you are trying to manipulate should be available by that time.

Put this script at the bottom of your document (before </body> tag):

 <script type='text/javascript'>
    var yourDiv = document.getElementById('yourDivId');
    yourDiv.innerText = 'something set into div now'; // If you want to set pure text;
    yourDiv.innerHtml = '<p>To set HTML inside an element</p>';
 </script>

Pavan- How can you set the value before element get created on page...Document.ready method set the value once the element get created I dont understand what you are trying to do but according to me if there is no element in dom how you can set the text in it.

You can try out OnLoad() event of the body element may do your task.

EDIT

Try this : not much sure about this

function test () {     
       alert(this.readyState);  
}  

<div onreadystatechange="test();"></div>

Jquery's document ready is tied to the DOMContentLoaded event. You really can't reliably manipulate the DOM before that event is fired.

<div><script type="text/javascript">document.write('content')</script></div>

You'll read sometimes that document.write should be avoided but this case would be an exception.

document ready is what you want since it starts when the DOM is ready, but before all the HTML elements like such as div is loaded

Use

document.getElementById("successAndErrorMessages").innerHTML="someContent";

发布评论

评论列表(0)

  1. 暂无评论