简体   繁体   English

我的 css 无法在脚本中工作

[英]My css won't work within a script

In the html file:在 html 文件中:

<html>
  <head>
     <script type="text/javascript" src="/images/files/js/callback.js"></script>
  </head>
</html>

in the callback.js file:在 callback.js 文件中:

$(document).ready(function() {
document.write('<script type="text/javascript" src="/images/files/css/style.css" ></script>');
document.write('<link src="/images/files/js/core.js">');
}

About some reason it will add al the text of the callback.js file into my page but won't load the style.css and the core.js, So when I visit the page the elements of callback.js will be in the head element but they don't work on the page, so I have a page without css and js出于某种原因,它会将 callback.js 文件的所有文本添加到我的页面中,但不会加载 style.css 和 core.js,因此当我访问该页面时,callback.js 的元素将位于头部元素,但它们在页面上不起作用,所以我有一个没有 css 和 js 的页面

second thing I want to achieve is that if it has load all the content of the callback.js it will remove the link to callback.js so that file isn't visible anymore我想要实现的第二件事是,如果它加载了 callback.js 的所有内容,它将删除指向 callback.js 的链接,以便该文件不再可见

The reason I want to do this is in javascript/jquery is because I hate php and I've a lot of pages with exactly the same links in it.我想这样做的原因是在 javascript/jquery 中,因为我讨厌 php 并且我有很多页面中包含完全相同的链接。

$(document).ready(function() {

   /* creating script and link elements */

   var style = $('<link />', {
       'href': '/images/files/css/style.css',
       'rel': 'stylesheet'
   });
   var core  = $('<script />', { 
       'src': '/images/files/js/core.js'
   });


   /* append style and core */

   var head  = $('head');
   style.appendTo(head);
   core.appendTo(head);

});

Change改变

document.write('<script type="text/javascript" src="/images/files/css/style.css" ></script>');

to,到,

document.write('<link rel="stylesheet" type="text/css" href="/images/files/css/style.css" >');

You write :你写 :

document.write('<script type="text/javascript" src="/images/files/css/style.css" ></script>');

You are trying to include a CSS file in a script tag.您正在尝试在脚本标记中包含 CSS 文件。 Use instead :改用:

document.write('<link rel="stylesheet" href="/images/files/css/style.css"/>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM