Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the css properties before adding to dom?

Tags:

html

jquery

I have a code like this, this is an example code,

<style>
    .divStyle
    {
        border: 2px solid gray;
    }
</style>
<script>
    $(document).ready(function () {
        var div = $("<div class='divStyle'></div>");
        var border = div.css("border");
    });
</script>

the border returns empty string. how to get the border value ?

like image 636
BalaKrishnan웃 Avatar asked Sep 28 '12 10:09

BalaKrishnan웃


People also ask

What is :: before in DOM?

The ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert.

Can CSS modify DOM?

No, CSS does not change the DOM.


1 Answers

You cant get it done without inserting it to the DOM but here is a trick I can think of and it's that insert it into dom as hidden, get the css property and remove it.

$(document).ready(function () {
    var div = $("<div class='divStyle'></div>").css('display','none').appendTo('body');
    var border = div.css('border');
    div.remove();
    alert(border);
});

DEMO.

like image 107
The Alpha Avatar answered Nov 15 '22 05:11

The Alpha