【JS】測試:HTML頁面引入另一個HTML頁面,同名ID會衝突嗎,jsid
今天被問到一個問題,就是當頁面HTML頁面引入了另外一個HTML頁面,恰好2個頁面具有相同的ID,那麼需要用ID來處理一些行為的時候會發生衝突或者報錯嗎?
我用了iframe來引入另外一個HTML頁面。
先準備好材料:1個HTML頁面(含iframe標籤,我命名為test.html),再加一個HTML頁面(作為iframe頁面的src源,這個頁面我命名為iframe.html)。兩個頁面的內容分別如下:
test.html:
<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><title>測試</title><meta name="description" content=""><meta name="keywords" content=""><link href="" rel="stylesheet"></head><body> <p id="sameId">這是測試頁</p> <p>這是測試頁哦</p> <p>這是測試頁啊</p> <iframe src="iframe.html" width="400" height="400" scrolling="no"></iframe> <script type="text/javascript"> window.onload=function(){ var getId=document.getElementById('sameId'); getId.style.color='red'; }; </script></body></html>
iframe.html:
<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><title>iframe內容頁</title><meta name="description" content=""><meta name="keywords" content=""><link href="" rel="stylesheet"></head><body> <p id="sameId">這是iframe內容頁</p> <p>這是iframe內容頁哦</p> <p>這是iframe內容頁啊</p></body></html>
測試結果:
結果顯示,iframe架構頁面裡同樣被標記為id="sameId"的段落字型顏色並沒有變成紅色,所以即使引入了另一個html頁面,兩個頁面的id相同也不會發生衝突。
PS:這隻是使用iframe方法嵌入另一個網頁的測試,其他的方法我還沒有嘗試喲。
Author:致知
Sign:路漫漫其修遠兮,吾將上下而求索。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。