2013-02-25 1 views
0

if 문이있는 함수가 있습니다. 문이 true이면 innerHTML을 사용하여 문서에 다시 씁니다. innerHTML 내에서 p 태그와 텍스트를 추가 할 수 있지만 <a href> 행을 추가하자마자 "Uncaught SyntaxError : Unexpected identifier"오류가 발생합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 이 오류는 다음 코드와 calage() 기능에 있습니다 :innerHTML 문에 href 링크를 만들 수 없습니다.

if((calyear == 2010 && calmon > 10) || (calyear == 2011 && calmon < 11)) { 
     console.log("They should be in the 2YO"); 
     document.getElementById("demo").innerHTML="<p>Your student would more than likely enter the 2YO class. The 2YO students will have class at the Nanshan campus. To apply, please fill out the following application.</p> <a href="http://qsishekou.org" target="_blank">QSI Shekou!</a>"; 
     } 

전체 HTML 파일은 다음과 같습니다. 도와 줘서 고마워!

<!DOCTYPE HTML> 

<html> 
    <head> </head> 
    <body> 
<table cellpadding=0 cellspacing=0 style="width:95%;" align="center"> 
<tr><td align="center" class="abouttabletext"><br> 
<script type="text/javascript"> 
var startyear = "1995"; 
var endyear = "2013"; 
var dat = new Date(); 
var curday = dat.getDate(); 
var curmon = dat.getMonth()+1; 
var curyear = dat.getFullYear(); 
function checkleapyear(datea) 
{ 
    if(datea.getYear()%4 == 0) 
    { 
     if(datea.getYear()% 10 != 0) 
     { 
      return true; 
     } 
     else 
     { 
      if(datea.getYear()% 400 == 0) 
       return true; 
      else 
       return false; 
     } 
    } 
return false; 
} 
function DaysInMonth(Y, M) { 
    with (new Date(Y, M, 1, 12)) { 
     setDate(0); 
     return getDate(); 
    } 
} 
function datediff(date1, date2) { 
    var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(), 
    y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate(); 
    if (d1 < d2) { 
     m1--; 
     d1 += DaysInMonth(y2, m2); 
    } 
    if (m1 < m2) { 
     y1--; 
     m1 += 12; 
    } 
    return [y1 - y2, m1 - m2, d1 - d2]; 
} 

function calage() 
{ 
    var calday = document.birthday.day.options[document.birthday.day.selectedIndex].value; 
    var calmon = document.birthday.month.options[document.birthday.month.selectedIndex].value; 
    var calyear = document.birthday.year.options[document.birthday.year.selectedIndex].value; 
    if(curday =="" || curmon=="" || curyear=="" || calday=="" || calmon=="" || calyear=="") 
    { 
     alert("please fill all the values and click go -"); 
    } 
    else 
    { 
     var curd = new Date(curyear,curmon-1,curday); 
     var cald = new Date(calyear,calmon-1,calday); 
     var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0); 
     var dife = datediff(curd,cald); 
     var monleft = (dife[0]*12)+dife[1]; 
     var secleft = diff/1000/60; 
     var hrsleft = secleft/60; 
     var daysleft = hrsleft/24; 
     var as = parseInt(calyear)+dife[0]+1; 
     var datee = diff/1000/60/60/24; 
    } 
    if((calyear == 2010 && calmon > 10) || (calyear == 2011 && calmon < 11)) { 
     console.log("They should be in the 2YO"); 
     document.getElementById("demo").innerHTML="<p>Your student would more than likely enter the 2YO class. The 2YO students will have class at the Nanshan campus. To apply, please fill out the following application.</p> <a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a>"; 
     } 
     if((calyear == 2009 && calmon > 10) || (calyear == 2010 && calmon < 11)) { 
     console.log("They should be in the 3YO"); 
     return 3; 
    } 
    if((calyear == 2008 && calmon > 10) || (calyear == 2009 && calmon < 11)) { 
     console.log("They should be in the 4YO"); 
    } 
    if((calyear == 2007 && calmon > 10) || (calyear == 2008 && calmon < 11)) { 
     console.log("They should be in the 5YO"); 
    } 
    if((calyear == 2006 && calmon > 10) || (calyear == 2007 && calmon < 11)) { 
     console.log("They should be in the 6YO"); 
    } 
    if((calyear == 2005 && calmon > 10) || (calyear == 2006 && calmon < 11)) { 
     console.log("They should be in the 7YO"); 
    } 
    if((calyear == 2004 && calmon > 10) || (calyear == 2005 && calmon < 11)) { 
     console.log("They should be in the 8YO"); 
    } 
    if((calyear == 2003 && calmon > 10) || (calyear == 2004 && calmon < 11)) { 
     console.log("They should be in the 9YO"); 
    } 
    if((calyear == 2002 && calmon > 10) || (calyear == 2003 && calmon < 11)) { 
     console.log("They should be in the 10YO"); 
    } 
    if((calyear == 2001 && calmon > 10) || (calyear == 2002 && calmon < 11)) { 
     console.log("They should be in the 11YO"); 
    } 
    if((calyear == 2000 && calmon > 10) || (calyear == 2001 && calmon < 11)) { 
     console.log("They should be in the 12YO"); 
    } 
    if((calyear == 1999 && calmon > 10) || (calyear == 2000 && calmon < 11)) { 
     console.log("They should be in the 13YO"); 
    } 
    if((calyear == 1998 && calmon > 10) || (calyear == 1999 && calmon < 11)) { 
     console.log("They should be in the 14YO"); 
    } 
    if((calyear == 1997 && calmon > 10) || (calyear == 1998 && calmon < 11)) { 
     console.log("They should be in the 15YO"); 
    } 
    if((calyear == 1996 && calmon > 10) || (calyear == 1997 && calmon < 11)) { 
     console.log("They should be in the 16YO"); 
    } 
    if((calyear == 1995 && calmon > 10) || (calyear == 1996 && calmon < 11)) { 
     console.log("They should be in the 17YO"); 
    } 
    if(calyear == 1995 && calmon < 11) { 
     console.log("Your child is too old to attend QSI"); 
    } 
} 
</script> 
<form name="birthday" action=""> 
Date<select name="day" size="1"> 
<script type="text/javascript"> 
for(var j=1;j<32;j++) 
    document.write("<option value="+j+">"+j+"</option>"); 
</script></select>&nbsp; 
Month<select name="month" size="1"> 
<script type="text/javascript"> 
for(var i=1;i<13;i++) 
document.write("<option value="+i+">"+i+"</option>"); 
</script></select>&nbsp; 
Year 
<select name="year" size="1"> 
<script type="text/javascript"> 
for(var k=startyear;k<endyear;k++) 
document.write("<option value="+k+">"+k+"</option>"); 
</script></select> 
<br><br> 
<input name="start" onclick="calage()" value="Calculate" type="button"> 
</form> 
</td></tr></table> 
<div id="demo"> 

</div> 
</body> 
</html> 
+0

은 이스케이프 문자를 사용합니다. – Nagri

답변

0

이 시도 : 당신은 같은

 document.getElementById("demo").innerHTML="<p>Your student would more than likely enter the 2YO class. The 2YO students will have class at the Nanshan campus. To apply, please fill out the following application.</p> <a href='http://qsishekou.org' target='_blank'>QSI Shekou!</a>"; 

는 ""로 문자열을 둘러싸 - 그래서 속성을 둘러싸고 '사용합니다. 예를 들어

:

따라서, 예외를 당신이 문자열을 닫고 다음 연결할하지 않기 때문에 오류가 발생합니다

var str = "hello, I am" gonna go" here"; 
.

때문에,

var str = "hello, I am' gonna go' here"; 

이 적절할 것이다.

또는

var str = "hello, I am" + "gonna go here"; 

또는 큰 따옴표를 사용하는 경우, 다음을 백 슬래시.

var str = "hello, I am\" gonna go\" here"; 
+0

와우. 그게 완벽 했어! 이중 따옴표 안의 큰 따옴표에 대해서는 생각하지 않았습니다. 고맙습니다! – Nathan

+1

나는 답장을받은 후 5 분을 기다려야 "대답"이라고 표시 할 수 있습니다. 내가 지금 해보 죠 ... 좋아, 너 거기 간다. 다시 한 번 감사드립니다! – Nathan

관련 문제