简体   繁体   English

CSS样式表未添加

[英]css stylesheet not getting added

I load any web page. 我加载任何网页。 Then open firebug console and run the below javascript, which creates a link element in the head of the page. 然后打开firebug控制台并运行下面的javascript,它在页面顶部创建一个link元素。 the code is below. 代码如下。

    var s = document.createElement('link');
    s.setAttribute('href', 'file:///home/simha/.public_html/new1.css');
    s.setAttribute('rel', 'stylesheet');
    s.setAttribute('type', 'text/css');
    document.getElementsByTagName('head')[0].appendChild(s);
    alert('Stylesheet injected!');

the content of the file:///home/simha/.public_html/new1.css 文件的内容:///home/simha/.public_html/new1.css

body { background-color: #0000ee !important; }

I run the code in the firebug console and the following appears in the head of the html 我在firebug控制台中运行代码,并且以下内容出现在html的标题中

<link type="text/css" rel="stylesheet" href="file:///home/simha/.public_html/new1.css">

But there no change in the background color (change to blue) of the body. 但是,主体的背景颜色没有变化(变为蓝色)。

I checked the css rule independently from editing the css in firebug, the background color changes to blue. 我独立于在萤火虫中编辑CSS来检查CSS规则,背景颜色变为蓝色。

I have apache web server installed. 我已经安装了Apache Web服务器。 So i also tried instead of "file:///home/simha/.public_html/new1.css" to "localhost/~simha/new1.css" still it does not work. 因此,我也尝试将“ file:///home/simha/.public_html/new1.css”替换为“ localhost /〜simha / new1.css”,但它仍然无法正常工作。

I am using firefox browser. 我正在使用Firefox浏览器。

这是因为出于安全原因,仅当文档也已从本地URI加载时,才可以从本地URI方案(例如file: :)加载外部资源。

尝试这个,

 s.setAttribute('href', 'new1.css');

Your stylesheet named new1.css needs to be referenced in relation to the server's document root. 您需要参照服务器的文档根目录引用名为new1.css样式表。

So you're using Apache, which means you setup the apache2.conf (or httpd.conf, etc) as well as the site conf file(s). 因此,您使用的是Apache,这意味着您需要设置apache2.conf(或httpd.conf等)以及站点conf文件。 The path you used in this config for DocumentRoot will be the reference point. 您在此配置中为DocumentRoot使用的路径将成为参考点。 From there you simply put /path/to/new1.css . 从那里,您只需将/path/to/new1.css放进去。

So in your case, 所以就你而言

file:///home/simha/.public_html/new1.css

will translate to 将翻译成

/new1.css

So your final code will be 所以您的最终代码将是

<link type="text/css" rel="stylesheet" href="/new1.css">

When you put file:/// prefixing an address, it's not using the server filesystem to load the file, it's using the client/user. 当您在file:///前面加一个地址时,它不是在使用服务器文件系统来加载文件,而是在使用客户端/用户。 This is due to the blank string where the hostname should be - file://_BLANKHOSTNAME_/ . 这是由于主机名应为空白字符串file://_BLANKHOSTNAME_/ Here's more info if you're curious. 如果您好奇的话,这里有更多信息。

For your localhost/new1.css to work, it needs to be prefixed with http:// like this: 为了使localhost/new1.css正常工作,需要使用http://作为前缀:

href="http://localhost/new1.css"

Otherwise it's interpreting localhost as if it were a directory name and not the hostname. 否则,它将localhost解释为目录名而不是主机名。

I think the problem is with file: or localhost in firefox, I saved the file in dropbox and used that link it worked. 我认为问题出在file:或Firefox中的localhost,我将文件保存在保管箱中并使用了有效的链接。

    var s = document.createElement('link');
    s.setAttribute('href', 'https://dl-web.dropbox.com/get/new1.css?w=AACLoomOT900PfGVqEuu9rHP4NlewdOq0KaSZbhzmgyG1A&dl=1');
    s.setAttribute('rel', 'stylesheet');
    s.setAttribute('type', 'text/css');
    document.getElementsByTagName('head')[0].appendChild(s);
    alert('Stylesheet injected!');

But is there any setting in the linux for apache and file to make it work locally. 但是linux中是否有针对apache和file的设置以使其在本地工作。

One possible problem might be the path you have 一个可能的问题可能是您的路径

file:///home/simha/.public_html/new1.css

When you allow per user directories in Apache, it is usually public_html without a leading dot. 当您在Apache中允许每个用户目录时,通常为public_html 带前导点。 So, when you rename the directory to /home/simha/public_html , http://localhost/~simha/new1.css should work as expected. 因此,当您将目录重命名为/home/simha/public_htmlhttp://localhost/~simha/new1.css应该可以正常工作。

Your code works as is, at least when you use an absolute URL 您的代码按原样运行,至少在使用绝对URL时

<button type="button" class="btn btn-info">Info</button>

In the Javascript, I replaced your CSS with the publicly accessible Bootstrap CSS 在Javascript中,我将CSS替换为可公开访问的Bootstrap CSS

s.setAttribute('href', 'http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css');

everything else is left as is, see JSFiddle 其他一切都保持不变 ,请参阅JSFiddle

As you can see, the button is coloured like the Info button in the docs at Bootstrap - Buttons . 如您所见,按钮的颜色类似于Bootstrap-Buttons中文档中的Info 按钮

If you did not solve this problem yet, 如果您尚未解决此问题,

  1. Serve all static content from a (http) server of your choice. 从您选择的(http)服务器提供所有静态内容。 Do not use file system paths. 不要使用文件系统路径。 Firefox wont load them ( in a web page context ) Firefox不会加载它们(在网页上下文中)
  2. Try to put all the static resources along with your html, and serve them with relative url. 尝试将所有静态资源与您的html一起放置,并使用相对网址进行投放。

Now try this thing. 现在尝试这个东西。 open view source, and check if the css path is clickable and is fetching the content according to your expectation. 打开视图源,然后检查css路径是否可单击,并且是否根据您的期望获取内容。 If not, directly access that file from address bar of the browser. 如果不是,请直接从浏览器的地址栏中访问该文件。 (Make sure your localhost is running and you have put all your files related to this in the web server - I normally use /var/www/). (确保您的本地主机正在运行,并且已将与此相关的所有文件放入Web服务器-我通常使用/ var / www /)。 Use the path that works like this in your html too. 在您的html中也使用类似这样的路径。 (no file:// protocol, remember). (没有file://协议,请记住)。

If you are not success by these, WWJD. 如果您不能成功,WWJD。

Also tell us when you are successful. 告诉我们您什么时候成功。

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

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