2017-01-18 171 views
0

我想問是否可以檢查JQuery,如果div有一定的屬性,並且如果有特定的值。所以借這個例子:如何檢查div是否具有特定值的特定css屬性?

<div id="foo" style="width: 100%" /> 

我需要檢查,如果DIV foo有CSS屬性width,如果這有100%值。

我能做些什麼來實現這個目標?

+0

http://stackoverflow.com/questions/24591327/jquery-check-if-element-has-a-specific-style-property-display – cloned

+0

@Richard不幸的是這個問題給出了當涉及到寬度計算的樣式 - 問題會更復制這個:http://stackoverflow.com/questions/16126670/return-css-height-with-jquery-not-computed-but-declared – Pete

+0

無論如何 - 你需要使用原生的js對象並使用樣式:'$('#foo')。get(0).style.width' – Pete

回答

2

如果您嘗試選擇基於內聯樣式屬性的元素,則可以使用屬性包含選擇器,但當defenition中存在額外空間時,可能會失敗。

if (('#foo[style*="width: 100%"]').length) 
 
    alert('true')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="foo" style="width: 100%" />


,以解決空間問題,您可以使用正則表達式與樣式屬性RegExp#test方法。

if (/\bwidth\s?:\s?100%/.test($('#foo').attr('style'))) 
 
    alert('true')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="foo" style="width: 100%" />

+0

uhm,nope,我試圖檢查div'foo'有'寬度'屬性,如果這個h ($('#foo')。css('width')=='100'){alert(true); }'但似乎沒有工作 – AgainMe

+1

@AgainMe:updated –

+1

好吧,這是足夠的'長度'檢查這一點,從來沒有這樣做。謝謝 – AgainMe

相關問題