nameContent
only exists within the first()
function, as you defined it within the first()
function.
To make its scope broader, define it outside of the functions:
var nameContent;
function first(){
nameContent=document.getElementById('full_name').value;
}
function second() {
first();
y=nameContent; alert(y);
}
second();
A slightly better approach would be to return
the value, as global variables get messy very quickly:
function getFullName() {
return document.getElementById('full_name').value;
}
function doStuff() {
var name = getFullName();
alert(name);
}
doStuff();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…