简体   繁体   English

JavaScript window.onload

[英]JavaScript window.onload

<!DOCTYPE html>  
<html>  
<head>  

    <script type="text/javascript" >
                 
               function changeTitle() {                      
                  if (document.getElementById('myText').value === "") {                     
                     document.getElementById('title').innerHTML = "Welcome to JavaScript";  
                     alert("Enter a valid text title");  
                     return;                      
                  } else {                        
                  document.getElementById('title').innerHTML = document.getElementById('myText').value;  
                  
                  }  
               }                                
      </script>           
</head>          
<body>  
    <h1 id="title">Welcome to JavaScript</h1>      
    <p> Hello! This is my first JavaScript example on Microsoft Visual Studio Express Edition 2013.</p>          
    <p>  
        <input id="myText" type="text"/>  
        <input type="submit" onclick="changeTitle();" value="Click me!"/>  
    </p>      
</body>  
</html>  

In this little example, I want to save the initial innerHTML of the h1 tag (id=title) to a variable after loading the page for the first time.在这个小例子中,我想在第一次加载页面后将 h1 标记 (id=title) 的初始 innerHTML 保存到一个变量中。

And then instead of the document.getElementById('title').innerHTML = 'Welcome to JavaScript' which is inside the if statement, I want to substitute that above mentioned variable with the phrase "Welcome to JavaScript ".然后,而不是 if 语句中的document.getElementById('title').innerHTML = 'Welcome to JavaScript' ,我想用短语“Welcome to JavaScript ”替换上面提到的变量。

My main intention is, wwhenever some one leaves the textbox ( id=myText ) blank and click on the submit button, script should replace the innerHTML of the h1 tag ( id=title ) to the initial value that was there in the first page load and pop out that alert box.我的主要意图是,每当有人将文本框( id=myText )留空并单击提交按钮时,脚本应将 h1 标记( id=title )的 innerHTML 替换为第一页加载中的初始值并弹出那个警告框。 (Maybe user has changed the innerHTML of the h1 before, but the script should replace it to the initial value that was there in the first page load). (也许用户之前更改了 h1 的 innerHTML,但脚本应该将其替换为第一个页面加载时的初始值)。

You can declare a hidden input.您可以声明一个隐藏的输入。

<input type="hidden" id="initialTitle" value=""/>

and populate the hidden field value via body onload JS function like.并通过 body onload JS function 填充隐藏字段值。

function setInitialTitle() { document.getElementById('initialTitle').value = document.getElementById('title').innerHTML }


<body onload="setInitialTitle()";>

And in changeTitle() function rewrite if block as.并在 changeTitle() function 中重写 if 块为。

document.getElementById('title').innerHTML = document.getElementById('initialTitle').value;

Just use onload="changeTitle()"只需使用onload="changeTitle()"

here is a demo: http://jsfiddle.net/nn007/cKk4U/1/这是一个演示: http://jsfiddle.net/nn007/cKk4U/1/

Try this instead of your body tag....试试这个而不是你的身体标签....

<body onload="changeTitle();">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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