通过JS在HTML页面代码中插入CSS链接的HTML [英] Inserting CSS-linked HTML in HTML page code via JS

查看:463
本文介绍了通过JS在HTML页面代码中插入CSS链接的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法访问页面的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 =你好! ;

output.appendChild(ele);

我想用这个基础插入一个允许从一个CSS集切换的按钮



非常感谢

解决方案

pre> window.onload = function(){
var output = document.getElementById('main_co');
var i = 1;
var val =;
//将所有href的切换到另一个路径
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)//如果我是1
{
if(!document.getElementById('timedrpact01'+ i)), {
var ele = document.createElement(div); ele.setAttribute( ID, timedrpact01 + I);
ele.setAttribute(class,inner);
ele.innerHTML =你好! ;
var button = document.createElement('button');

if(button.addEventListener){
button.addEventListener('click',switchStyleSheet);
}
else {
button.attachEvent('click',switchStyleSheet);
}

output.appendChild(button);
output.appendChild(ele);
}
}
}


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

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

这篇关于通过JS在HTML页面代码中插入CSS链接的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆