Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用javascript中的值替换.innerhtml文本_Javascript_Html_Cookies_Innerhtml - Fatal编程技术网

用javascript中的值替换.innerhtml文本

用javascript中的值替换.innerhtml文本,javascript,html,cookies,innerhtml,Javascript,Html,Cookies,Innerhtml,我是一个刚开始的网站程序员,我一直在用用户名替换innerhtml文本。如果网站加载,而用户没有输入任何姓名,他会收到一条消息,要求输入他的姓名。在一个教程中,他们使用了一个警告来表示欢迎,但我不想使用页面上的文本。我尝试将.innerhtml与值一起使用,但没有成功。我在网上也找不到任何东西。有人知道该怎么做吗?我这样写代码: <script> function setCookie(cname,cvalue,exdays) {

我是一个刚开始的网站程序员,我一直在用用户名替换innerhtml文本。如果网站加载,而用户没有输入任何姓名,他会收到一条消息,要求输入他的姓名。在一个教程中,他们使用了一个警告来表示欢迎,但我不想使用页面上的文本。我尝试将.innerhtml与值一起使用,但没有成功。我在网上也找不到任何东西。有人知道该怎么做吗?我这样写代码:

<script>
          
function setCookie(cname,cvalue,exdays) {

                var d = new Date();

                d.setTime(d.getTime() + (exdays*24*60*60*1000));
                var expires = "expires=" + d.toGMTString();
                document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
            }
            function getCookie(cname) {
                var name = cname + "=";
                var decodedCookie = decodeURIComponent(document.cookie);
                var ca = decodedCookie.split(';');
                for(var i = 0; i < ca.length; i++) {
                    var c = ca[i];
                    while (c.charAt(0) == ' ') {
                        c = c.substring(1);
                }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
                }
            }
            return "";
            }
            function checkCookie() {
                var user=getCookie("username");
                if (user != "") {
                    "document.getElementById("welcomemessage").innerhtml = "(user)";
                } else {
                    user = prompt("What is your name?","");
                    if (user != "" && user != null) {
                        setCookie("username", user, 30);
                    }
                    
                }
            }
    </script>
<p id="welcomemessage"></p>`

函数setCookie(cname、cvalue、exdays){
var d=新日期();
d、 设置时间(d.getTime()+(exdays*24*60*60*1000));
var expires=“expires=“+d.togmString();
document.cookie=cname+“=”+cvalue+”;“+expires+”;path=/”;
}
函数getCookie(cname){
变量名称=cname+“=”;
var decodedCookie=decodeURIComponent(document.cookie);
var ca=decodedCookie.split(“;”);
对于(变量i=0;i

`

使用模板文字:

document.getElementById("welcomemessage").innerHTML = `(${user})`

删除
文档开头的引号。getElementById
,将
innerhtml
更改为
innerhtml
,并设置我上面显示的值。

开发人员工具将指出错误。学会在浏览器中使用开发人员工具。这很有效,谢谢!
document.getElementById("welcomemessage").innerText = "(" + user + ")";
document.getElementById("welcomemessage").innerHTML = `(${user})`