[英]How to append the variable value to the element using document.write
I am trying to append the variable value to the element 我试图将变量值附加到元素
what i am doing is i am taking the input body onload by using javascript prompt. 我正在做的是我正在使用javascript提示输入正文onload。
now i am storing the input value to one variable. 现在我将输入值存储到一个变量。
now i want to append the variable value to this element 现在我想将变量值附加到此元素
<a href="#" class="uname">Welcome User!</a>
in place of welcome user,i want to get the input variable value. 代替欢迎用户,我想获得输入变量值。
here is the code i am using.. 这是我正在使用的代码..
function inpt(){
var person=prompt("Please enter your name","");
if (person!=null && person!="")
{
document.write(person);
}
}
How can i do this?? 我怎样才能做到这一点??
<a href="#" id="uname" class="uname">Welcome User!</a>
js JS
function inpt(){
var person=prompt("Please enter your name","")
uname = document.getElementById("uname");
if (person!=null && person!="")
{
uname.innerHTML = 'Welcome '+person+ '!'
//document.write(person);
}
}
first in my opinion you should give the link an id. 首先在我看来你应该给链接一个id。 eg.
例如。
<a href="#" class="uname" id="uname">Welcome User!</a>
then instead of writing 然后而不是写作
document.write(person);
you should use 你应该使用
document.getElementById('uname').innerHTML = person;
hope that helps. 希望有所帮助。
try this 尝试这个
function inpt(){
var person=prompt("Please enter your name","");
if (person!=null && person!="")
{
$('.uname').html(person);
}
}
Use Javascript Window.onload function which is fired when the entire page loads. 使用Javascript Window.onload函数,该函数在整个页面加载时触发。
Javscript code: Javscript代码:
<script>
window.onload=function(){
var person=prompt("Please enter your name","");
if (person!=null && person!="")
{
$("#uname").append(person);
}
}
</script>
HTML code: HTML代码:
<a href="#" id="uname">Welcome</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.