[英]Inserting CSS-linked HTML in HTML page code via JS
我無權訪問頁面的HTML(它們是動態程序構建的)。 我確實有權訪問鏈接到的JS頁面。
例如,我可以做這樣的事情,它可以工作:
window.onload=function(){
var output = document.getElementById('main_co');
var i=1;
var val="";
while(i<=1)
{ if(!document.getElementById('timedrpact01'+i))
{
var ele = document.createElement("div"); ele.setAttribute("id","timedrpact01"+i);
ele.setAttribute("class","inner");
ele.innerHTML=" Hi there!" ;
output.appendChild(ele);
我想以此為基礎插入一個按鈕,該按鈕允許從一個CSS集(調用了多個文件)切換到另一個_another路徑。
非常感謝
使用link
引用外部樣式表,如下所示:
<link rel="stylesheet" href="http://example.com/path-to-css">
因此,使用以下命令獲取適當的link
元素:
var css = document.getElementsByTagName("link")[0];
在這里,我們通過指定[0]
索引來掌握第一個可用link
。
然后,覆蓋href
屬性以將其指向新路徑。
css.setAttribute("href", "http://example.com/path-to-css");
window.onload=function(){
var output = document.getElementById('main_co');
var i=1;
var val="";
//switch all the href's to another path
var switchStyleSheet = function() {
var links = document.getElementsByTagName("link");
for(var i=0; lkC = links.length; i < lkC; i++)
links[0].href = links[0].href.replace('path_to_file', '_path_to_file');
};
while(i<=1) //while is not required here, if i is 1
{
if(!document.getElementById('timedrpact01'+i)) {
var ele = document.createElement("div"); ele.setAttribute("id","timedrpact01"+i);
ele.setAttribute("class","inner");
ele.innerHTML=" Hi there!" ;
var button = document.createElement('button');
if(button.addEventListener) {
button.addEventListener('click', switchStyleSheet);
}
else {
button.attachEvent('click', switchStyleSheet);
}
output.appendChild(button);
output.appendChild(ele);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.