via logo html,Inserting CSS-linked HTML in HTML page code via JS

问题

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

回答1:

The external stylesheets are referenced using link, as in:

So, get hold of the appropriate link element using:

var css = document.getElementsByTagName("link")[0];

Here, we got hold of the first link available by specifying the [0] index.

Then, overwrite the href attribute to point it to the new path.

css.setAttribute("href", "http://example.com/path-to-css");

回答2:

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);

}

}

}

来源:https://stackoverflow.com/questions/23995243/inserting-css-linked-html-in-html-page-code-via-js

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值