Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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创建母版页_Javascript_Jquery_Html_Asp.net - Fatal编程技术网

如何使用Javascript创建母版页

如何使用Javascript创建母版页,javascript,jquery,html,asp.net,Javascript,Jquery,Html,Asp.net,我正在尝试使用javascript创建母版页。我创建了包含html代码的Javascript对象,然后包括js和html文件。问题是我不能在html的头部插入css。jquery追加函数不起作用 你真的不需要在头部插入CSS,它就能为你的页面设计样式。我认为您可以在DOM中的任何位置包含标记,它应该可以工作 但是,如果必须将其包含在头部,请尝试以下操作: function addcssfile(cssfile){ var head = document.head || document

我正在尝试使用javascript创建母版页。我创建了包含html代码的Javascript对象,然后包括js和html文件。问题是我不能在html的头部插入css。jquery追加函数不起作用

你真的不需要在头部插入CSS,它就能为你的页面设计样式。我认为您可以在DOM中的任何位置包含标记,它应该可以工作

但是,如果必须将其包含在头部,请尝试以下操作:

 function addcssfile(cssfile){
    var head = document.head || document.getElementsByTagName('head')[0];
    var s = document.createElement('link');
    s.setAttribute('rel', 'stylesheet');
    s.setAttribute('href', cssfile);
    head.appendChild(s);
 }
如果只想附加CSS样式和属性,而不是实际的CSS文件,可以执行以下操作:

function addcss(css){
    var head = document.head || document.getElementsByTagName('head')[0];
    var s = document.createElement('style');
    s.setAttribute('type', 'text/css');
    if (s.styleSheet) {   // IE
        s.styleSheet.cssText = css;
    } else {                // the world
        s.appendChild(document.createTextNode(css));
    }
    head.appendChild(s);
 }

请提供一些代码。为什么你想以Grizzly Adams的名义将html页面作为.js文件包含?@AnchovyLegend我怀疑,尝试使用javascript创建母版页应该更像是尝试创建使用javascript的母版页。那个而且,jquery append函数正在工作可能应该是读取的,jquery append函数不工作。不过,在没有澄清的情况下,我们还有很多假设。这个问题看起来很严峻。@canon不需要代码,把它当作一个一般情况,就好像一个人想要在js的帮助下创建一个母版页一样。谢谢您的更正和澄清。@anchovyLegend否,我创建了包含HTML代码的变量,以便稍后使用javascript将它们注入其他HTML文件中。如果你有其他方法,我会很高兴的:我知道你可以在其他地方注入CSS,但我需要将它注入头部。。谢谢,我会尝试使用你的代码并回答:对不起,也许我的问题没有那么清楚。。我有一个js对象,它保存头部的html代码,其中也包含样式元素。我需要将包含html头部代码的var注入一个新的html文件中,特别是在头部。。。