2015-08-27 44 views
0
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Insert title here</title> 
<script src = "A1P1Q1.js"></script> 
</head> 
<body> 
<p id="a"></p> 
<p id="firstname"></p> 
<p id="details"></p> 

<script> 
    variable();  
</script> 

JavaScript代碼當我分離JavaScript和HTML文件,僅年齡顯示

function variable(){ 
    age = 24; 
    var fname = "sachin"; 
    lname = "hunur"; 
    details = [age, fname, lname]; 

    document.getElementById("a").innerHTML = "the age is" +" " +age; 
    document.getElementById("firstname").innerHTML = "first name is" + 
    " " + fname; 
    document.getElementById("details").innerHTML = "The user details are" + 
    "age : " + details[0] + "first name : " + details[1] + " last name : " 
    + details[2]; 
    return; 
} 

塊引用:所示的輸出是僅: 年齡爲24

如何獲得印刷的名字和詳細信息?

+2

是否有在JavaScript控制檯中的任何錯誤? – Barmar

+0

這聽起來像是在顯示第一個輸出後出現錯誤。 – Barmar

+5

它適用於我:http://jsfiddle.net/barmar/9ndt0gLt/ – Barmar

回答

-2

更正你的js代碼象下面這樣:

function variable() { 
    var age = 24, 
     fname = "sachin", 
     lname = "hunur", 
     details = new Array(
      age, 
      fname, 
      lname 
     ); 

    document.getElementById("a").innerHTML = "the age is" + " " + age; 
    document.getElementById("firstname").innerHTML = "first name is" + 
     " " + fname; 
    document.getElementById("details").innerHTML = "The user details are" + 
     "age : " + details[0] + "first name : " + details[1] + " last name : " 
     + details[2]; 
    return; 
} 
+1

您能否解釋一下您的解決方案有哪些不同以及它爲什麼可行? –