English 中文(简体)
从头部获得风格内容
原标题:get stylesheet content from head

I am trying to get a certain stylesheet from the head using javascript/jQuery. I do not want to use $( head ).get(0).innerHTML or similar cause the head is filled up with more than 30 script and link elements.

迄今为止,我曾尝试过这一尝试。

// that s the one
var $my_stylesheet = $(document.body.previousSibling).find( link:last );

//those did not work (result: "")
$my_stylesheet.get(0).innerHTML;
$my_stylesheet.text();
$my_stylesheet.html();

What can i do to get the stylesheet content from the head?

最佳回答

Try to request via ajax call:

var $my_stylesheet_url = $( head ).find( link:first ).attr( href );

var content;
$.get($my_stylesheet_url, function(data) {
    content = data;
    // do your staff here
});

Code: http://jsfiddle.net/kh2en/1/

问题回答

The actual way to access the stylesheets in Javascript is to reference document.styleSheets. If you have Chrome, or Firefox with Firebug, you can type that into the Javascript console and see what s available inside it.

Here are some good references to look at.

可以在不依赖GET申请的情况下获得风格表的内容。 关于联系内容的风格说明与 Java语。 好消息是,你能够用文件查阅这一目的。 风格或文件。 内容 ById(链接-对面)。

If you re going to use the document.stylesheets, you will see a map with all of your link tags. So, the fastest way is to add an id (or another unique flag).

现在,你可以选择使用以下文字的链接标签,并检索表格标的。

const sheet = document.getElementById( link-tag-id ).sheet;

With the link tag sheet, you can extract the css using the following snippet:

const css = Array.from(sheet.cssRules).map(rule => rule.cssText).join(   );

该文件将沿用你风格的每条条,将部分内容退回,并加入“cs”集团。

If you want to make a test, you can paste the code below on your console of any webpage, it will output the content of the first style sheet on the head:

const sheet = document.styleSheets[0];
const css = Array.from(sheet.cssRules).map(rule => rule.cssText).join(   );
console.log(css);

如果你使用<条码>和>;链接/>的内容与风格相链接,则你需要请非洲航天中心阅读其内容。 此外,这只会在同一个领域进行风格表的工作。

$( link[rel="stylesheet"] ).each(function (i, ele) {
  $.get($(this).attr( href ), function (data) {
    console.log(data);
  });
});

如果你通过<条码>和>; 字形与格;要素,你可以通过<条码>获取内容。

$( style ).each(function (i, ele) {
  console.log($(this).text());
});

Try this example... This will fetch the content of the first stylesheet

$.ajax({
     url : $(document).find( link )[0].href,
     dataType : text ,
     success :function(data){
        alert(data);
        }
});




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签