2011-03-14 66 views
2
var loc=window.location.href,width="500px",height="450px"; 
var a=document.createElement("div"); 

a.id="xyz"; 
a.style.position="fixed"; 
a.style.width="500px"; 
a.style.height="450px"; 
a.style.top="0px"; 
a.style.right="0px"; 

$('body').appendChild(a); 
a.innerHTML='<iframe id="submit" width="500px" height="450px" src="http://abc.html"><iframe>'; 

此代碼給出錯誤 表達式'$('body')。appendChild'[undefined]的結果不是函數。body.appendchild函數中的js腳本錯誤

請幫助我,因爲我無法弄清楚爲什麼這個錯誤發生在

回答

5

$爬行(「主體」)返回一個jQuery對象,而不是一個DOM節點。該對象沒有appendChild函數。 jQuery的當量是:

$('body').append(a); 

或者,使用get獲得DOM節點:

$('body').get(0).appendChild(a);