最近在寫頁面遇到了問題,一個dom好多地方用到,然后我就單獨寫了個html頁面,然后用iframe引入,但是,想獲取iframe里面input的value,獲取不到input,后面才知道原來js不能直接獲取iframe里面的元素,上代碼
引入公共html :deadSeedling.html
<IFRAME id="deadSdl" marginWidth=0 marginHeight=0 src="common/deadSeedling.html" frameBorder=0 width=100% height=150></IFRAME>
接下來就要獲取iframe的dom了
var childHtml = document.getElementById("deadSdl").contentWindow; //獲取以 HTML 對象來返回 iframe 中的文檔 chgNum =childHtml.document.getElementById('num').value; //獲取iframe里面id為num的input的value
打印chgNum如圖: