Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过JS在HTML页面代码中插入CSS链接的HTML_Javascript_Html_Css - Fatal编程技术网

Javascript 通过JS在HTML页面代码中插入CSS链接的HTML

Javascript 通过JS在HTML页面代码中插入CSS链接的HTML,javascript,html,css,Javascript,Html,Css,我无法访问页面的HTML(它们是程序动态构建的)。 我确实可以访问它链接到的JS页面 例如,我可以做一些像这样的事情,它是有效的: window.onload=function(){ var output = document.getElementById('main_co'); var i=1; var val=""; while(i<=1) { if(!document.getElementById('timedrpact01'+i))

我无法访问页面的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);
window.onload=function(){
var output=document.getElementById('main_co');
var i=1;
var val=“”;

而(i使用
链接引用外部样式表,如下所示:

<link rel="stylesheet" href="http://example.com/path-to-css">
在这里,我们通过指定
[0]
索引获得了可用的第一个
链接

然后,覆盖
href
属性以将其指向新路径

css.setAttribute("href", "http://example.com/path-to-css");
window.onload=function(){
var output=document.getElementById('main_co');
var i=1;
var val=“”;
//将所有href切换到其他路径
var switchStyleSheet=函数(){
var links=document.getElementsByTagName(“链接”);
for(var i=0;lkC=links.length;iwhile(我知道你是通过javascript搜索css的?)一直在到处寻找..谢谢!哦,我还需要q一些东西:谢谢!哦,我还需要q一些东西:有几个css行:如果我读得好,我应该:1:插入一组新的“链接”(每个css文件一个)但是我不知道如何编写这个脚本。2:那么我需要一个按钮来启动这个新集合,或者恢复到以前的集合。3:有没有办法做到这一点?确定顺序,然后使用适当的索引。对于按钮单击,使用单击处理程序来运行上述代码。
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);
      }
   }
 }