Javascript 如何让我的函数将用户名添加到h2标记
我是javascript新手,我很难让下面的函数正常工作。该函数用于在用户输入用户名后,将Javascript 如何让我的函数将用户名添加到h2标记,javascript,Javascript,我是javascript新手,我很难让下面的函数正常工作。该函数用于在用户输入用户名后,将用户名加上h2标题中已有的内容添加到网站中。它需要在用户输入姓名后立即显示 /* * this function will promtpt the user for their name; * store name in a variable called userName. * Use variable called phrase, which will hold h2 content * L
用户名
加上h2
标题中已有的内容添加到网站中。它需要在用户输入姓名后立即显示
/*
* this function will promtpt the user for their name;
* store name in a variable called userName.
* Use variable called phrase, which will hold h2 content
* Lastly write the "new" h2.
*/
function logIn() {
var userName = prompt("Please enter name");
pharse = document.getElementsByTagName('h2').innerHTML;
}
好吧,我来试试这个
// This solution depends on you having an id on your 'h2' element
var userName = prompt('Please enter name');
var headerElem = document.getElementById('yourIdHere');
var pharse = headerElem.innerHTML;
headerElem.innerHTML = pharse + ' ' + userName;
让我知道这是否是你想要的/如果你想解释我为什么这样做
这是一支可以使用的代码笔,你可以随便玩玩 好吧,我来试试这个
// This solution depends on you having an id on your 'h2' element
var userName = prompt('Please enter name');
var headerElem = document.getElementById('yourIdHere');
var pharse = headerElem.innerHTML;
headerElem.innerHTML = pharse + ' ' + userName;
让我知道这是否是你想要的/如果你想解释我为什么这样做
这是一支可以使用的代码笔,你可以随便玩玩 下面是一个如何实现这一点的示例: HTML
以下是如何实现这一点的示例: HTML
这确实对我有用。非常感谢。我们应该使用getElementsByTagName来进行课堂思考。这对我来说确实有效。非常感谢。我们应该使用getElementsByTagName作为类思维。
var h = document.querySelector('h2');
var userName = prompt("Please enter name");
h.innerHTML = h.innerHTML + ' / ' + userName
document.getElementById("name").innerHTML += prompt("What is your name?");