Help us understand the problem. What is going on with this article?

読み込んでいるCSS内に指定したルールが記述されているかを確かめるメソッドを書いてみた

More than 5 years have passed since last update.

このページでこのクラスを読み込んでたっけ?とCSSファイルを探さないとすぐには分からない場合があるとおもいます。そこで今回は、$.isRuleExist()というメソッドを作成してみました。以下がそのソースになります。

ソース

(function($){
     $.extend({
       isRuleExist:function(className){
              var classes = [];
              var sheets = document.styleSheets;
              var push = Array.prototype.push;
              for(var i = 0,n = sheets.length; i < n; i++){
                push.apply(classes, sheets[i].rules || sheets[i].cssRules);
              }
              for(var i = 0,n = classes.length; i < n; i++){
                 if(classes[i].selectorText == className){
                         return true;
                 }
             }
             return false;
          }
     });
})(jQuery);

使い方

$.isRuleExist(".entryList li");
もしCSSファイルにこのルールが存在すればtrueなければfalseを返します

949559ce16576d9948d84d3a6e5e487e.png

うーん、でも普通にChrome Dev Toolでリソースを調べた方が速いかも。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away