【JS】測試:HTML頁面引入另一個HTML頁面,同名ID會衝突嗎,jsid

來源:互聯網
上載者:User

【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:路漫漫其修遠兮,吾將上下而求索。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.