2011-10-03 10 views
9

私はmodernizrを使用して、:nth-childブラウザのサポートをテストしようとしていますが、それを行う方法がわかりません。:last-childをテストするhttp://jsfiddle.net/laustdeleuran/3rEVe/が見つかりましたが、私はそれを検出する方法を変更する方法がわかりません:nth-child:last-childをサポートしていないブラウザでも:nth-childをサポートしていないと思われるので、そのような使い方を考えていたのですが、私はわかりません)Modernizrを使ってn番目の子供をテストするには?

あなたは私を助けてくれますか?前もって感謝します!

私はちょうど検出する機能を書いた
+2

正直に言うと、それはブラウザが 'の両方をサポートしていますどちらかと考えるのは非常に合理的です:n番目の子()'と ':最後-child'、またはいずれかをサポートしていません。 – BoltClock

+0

このプラグインを試してみてください。http://selectivizr.com/
cssファイルのみの下に挿入してください! –

答えて

13

:あなたは、これがまたここに http://jsbin.com/epuxus/15

アクションで働く見ることができます

isNthChildSupported() ? console.log('yes nth child is supported') : console.log('no nth child is NOT supported'); 

:使用あなた

function isNthChildSupported(){ 
var result = false, 
    test = document.createElement('ul'), 
    style = document.createElement('style'); 
test.setAttribute('id', 'nth-child-test'); 
style.setAttribute('type', 'text/css'); 
style.setAttribute('rel', 'stylesheet'); 
style.setAttribute('id', 'nth-child-test-style'); 
style.innerHTML = "#nth-child-test li:nth-child(even){height:10px;}"; 
for(var i=0; i<3; i++){ 
    test.appendChild(document.createElement('li')); 
} 
document.body.appendChild(test); 
document.head.appendChild(style); 
    if(document.getElementById('nth-child-test').getElementsByTagName('li')[1].offsetHeight == 10) {result = true;} 
document.body.removeChild(document.getElementById('nth-child-test')); 
document.head.removeChild(document.getElementById('nth-child-test-style')); 
    return result; 
} 

のためにn番目の子のサポートをjQuery :nth-childと​​の間に違いがあります。

jQueryの:nth-childは、私はそこのセレクタをサポートするためにテストさModernizrセレクタプラグインでしたが、私はできません覚えているIE9、クロム、SafariやFirefoxでのjQueryをサポートする任意のブラウザでサポートされが、CSS :nth-childis supported

+3

あなたの答えをありがとう、それは動作しますが、もう少し見て回った後、私は実際にすべてのセレクタをテストするこのhttps://gist.github.com/441842を見つけました。 – javiervd

+1

私はこれが古いと知っていますが、他の誰かがIE8でこの機能を使用するときにエラーを受け取りますか? http://stackoverflow.com/questions/6631871/modifying-the-innerhtml-of-a-style-element-in-ie8 – doubleswirve

2

また、あなたの意思決定をありがとう、サポートされていないブラウザ

+0

を参照してください。これはうまくいきます。 – hellojebus

1

モフセンにCSS3セレクターのサポートを追加するためにSelectivizrを使用することができます。 誰かがjQueryのに必要がある場合:

function test(){ 

    var result = false, 
     test = $('<ul id="nth-child-test"><li/><li/><li/></ul>').appendTo($('body')), 
     style = $('<style type="text/css">#nth-child-test li:nth-child(even){height:10px;}</style>').appendTo($('head')); 

    if(test.children('li').eq(1).height() == 10) 
    result = true; 

    test.remove(); 
    style.remove(); 

    return result; 

}; 
+0

は魅力的に機能します – Alex

関連する問題