在js中加html,通过JS在HTML页面代码中插入CSS链接的HTML

I do not have access to the HTML of the pages (they are program-built dynamically).

I do have access to the JS page it is linked to.

For example I can do somethin like this and it works:

window.οnlοad=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);

I would like to use this basis insert a button that would allow to switch from one CSS set (there are several files invoked) to another _another path.

Many thanks

解决方案window.οnlοad=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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值