使用提示更改div内的文本 [英] Using a prompt to change text inside of a div

查看:104
本文介绍了使用提示更改div内的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有这个简化的代码来说明我在这个页面上要做的事情。我希望浏览器在加载页面时立即显示一个提示,询问他们的名字。一旦他们回答他们的名字,它就会使用该变量(名称)并将其写入ID为welcomeText的div中。它只是因为某些原因不起作用...请帮助谢谢。



继承我的代码。把它放在一个html索引中,以便阅读。

 < title>欢迎来到Validus< / title> 

< style>
#welcomeText {
font-family:Verdana;
font-size:12px;
颜色:黑色;
width:100px;
height:100px;
margin:0px auto;
}
< / style>

< script type =text / javascript>

var name = prompt(嗨!欢迎来到Validus!你叫什么名字?,Name);

document.getElementById(welcomeText)。innerHTML =嘿++ name +!+Welcome to validus ...;

< / script>

< / head>
< body>

< div id =welcomeText>
< / div>

< / body>


解决方案

window.onload 当页面完成加载时启动。

还有其他很多方法可以实现您需要的功能...



< >
  • window.onload = func;

  • window.addEventListener('load',func,false);
  • window.addEventListener('DOMContentLoaded',func,false);
  • 或者只是在你的body标签的末尾添加你的javascript。 < script>< / script>< / body>

  • window.addEventListener('DOMContentReady',func,false) ; 使用jquery的
  • ...


  • 最常见且兼容的是window.onload。




     <!doctype html> 
    < html>
    < head>
    < meta charset =utf-8>
    < title>欢迎使用Validus< / title>
    < style>
    #welcomeText {
    font-family:Verdana;
    font-size:12px;
    颜色:黑色;
    width:100px;
    height:100px;
    margin:0px auto;
    }
    < / style>
    < script type =text / javascript>
    window.onload = function(){
    var name = prompt(嗨!欢迎来到Validus!你叫什么名字?,Name);
    document.getElementById(welcomeText)。innerHTML = name;
    }
    < / script>
    < / head>
    < body>
    < div id =welcomeText>< / div>
    < / body>
    < / html>


    Ok so I have this simplified code of what I am trying to do on this page. I want the browser to display a prompt as soon as they load the page asking for their name. Once they answer what their name is it takes that variable (name) and writes it inside of the div with the id "welcomeText". It just won't work for some reason... Please help thanks.

    Heres my code. Put it all inside of a html index to make it easier to read.

    <title>Welcome to Validus</title>
    
    <style>
    #welcomeText {
             font-family:Verdana;
             font-size:12px;
             color:black;
             width:100px;
             height:100px;
             margin:0px auto;
    }
    </style>
    
    <script type="text/javascript">
    
     var name=prompt("Hey! Welcome to Validus! Whats your name?", "Name");
    
     document.getElementById("welcomeText").innerHTML = "Hey" + " " + name + "! " + "Welcome to validus...";
    
    </script>
    
    </head>
    <body>
    
    <div id="welcomeText">
    </div>
    
    </body>
    

    解决方案

    window.onload is launched when the page has finished loading.

    there are also other many ways to acomplish what u need...

    1. window.onload=func;
    2. window.addEventListener('load',func,false);
    3. window.addEventListener('DOMContentLoaded',func,false);
    4. or just append your javascript at the end of your body tag. <script></script></body>
    5. window.addEventListener('DOMContentReady',func,false);
    6. using jquery...

    the most common and compatible is window.onload.


    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Welcome to Validus</title>
    <style>
    #welcomeText {
     font-family:Verdana;
     font-size:12px;
     color:black;
     width:100px;
     height:100px;
     margin:0px auto;
    }
    </style>
    <script type="text/javascript">
    window.onload=function(){
     var name=prompt("Hey! Welcome to Validus! Whats your name?", "Name");
     document.getElementById("welcomeText").innerHTML = name;
    }
    </script>
    </head>
    <body>
    <div id="welcomeText"></div>
    </body>
    </html>
    

    这篇关于使用提示更改div内的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆