开发者

Selection of a specific inline style

开发者 https://www.devze.com 2023-02-15 11:02 出处:网络
how can I select only the divs with style \"right:200px\" in jquery? Example: <div class=\"test\" style=\"position:absolute; right:200px; top:10px;\"><p>Hello</p></div>

how can I select only the divs with style "right:200px" in jquery?

Example:

<div class="test" style="position:absolute; right:200px; top:10px;"><p>Hello</p></div>
<div class="test" style="position:absolute; right:300px; top:20px;"><p>Hello</p></div>
<div class="test" style="position:absolute; right:400px; top:70px;"><p>Hello</p></div>
<div class="test" style="position:absolute; right:200px; top:40px;"><p>Hello</p></div>
<div class="test" style="position:absolute; right:400px; top:100px;"><p>Hello</p></div>
<div class="test" style="position:absolute; right:200px; top:140px;"><p>Hello</p></div>


var div200 = $('.test').css('right');

I don't know how to select only the divs with "right:200px". I'm new to jquery. I tried hard but without any success开发者_如何学C:

Achim


var div200 = $('.test').filter(function(){
    return $(this).css('right') == "200px";
});


$('.test').filter(function(){
    return $(this).css('right') == "200px";
}).addClass('selected');

See example.


I don't know how to select only the divs with "right:200px".

This is probably possible somehow, but it's a bit against how jQuery and CSS are supposed to work. Consider putting the actual position information into classes:

div.p1 { position:absolute; right:200px; top:10px; }
div.p2 { position:absolute; right:300px; top:20px; }
....
etc.

and having a simplified div structure:

<div class="test p1"><p>Hello</p></div> 
<div class="test p2"><p>Hello</p></div>
etc. 

and then addressing each div using

var div1 = $('.test.p1');
var div2 = $('.test.p2');
etc.

that also makes you independent from specific numbers like 200px - the only place where you change it is in the style sheet.

0

精彩评论

暂无评论...
验证码 换一张
取 消