English 中文(简体)
j 表格和单项功能的准确性[复制]
原标题:jQuery and the accuracy of the css function [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
jQuery and margin: 0 auto

我有以下超文本:

<p style="margin-left:auto; margin-right:auto; width:200px;">Hello</p>

然后,我使用j Query获得margin-left。 同样:

alert(window.jQuery(element).css( margin-left ));

但是,它提醒注意<代码>0px,而不是预期的<代码>auto。

为什么情况如此,我如何获得汽车?

最佳回答

找到解决办法! 这一职能将很少发挥 j色挑选器的作用,扫描你所有的风格表,过滤影响所赋予的OMS要素的规则,将这些规则加以规范,并将一份附有特别安全局规则清单的物体从所涉所有遗产的相互作用中收回。

<style type="text/css">

    .fool{
        margin-left:200px;
    }

    #fool{
        margin-left:auto;
    }
</style>

<div id="fool" class="fool" style="font-size:100px; height:20px;"></div>

<script type="text/javascript">
    jQuery( document ).ready(function($){

        function get_inheritance_result(jQueryElement){
            var styleSheets = document.styleSheets, finalRules = {};
            for(var i in styleSheets) {
                var rules = styleSheets[i].rules || styleSheets[i].cssRules;
                for(var r in rules) {
                var selector = String(rules[r].selectorText);
                    if(selector.indexOf( : ) == -1){ //ignoring pseudo selectors, they will not matter here
                        if(jQueryElement.is(selector.toString())) {
                            console.log(selector.toString()); // Will log all the selectors that are affecting the particular DOM element
                            finalRules = $.extend(finalRules, rules[r].style);
                        }
                    }
                }
            }
            //taking any inline css and overriding it on top of our finalRules variable:
            var inlineCSS = jQueryElement.prop( style );
            for(i in inlineCSS){
                if(inlineCSS.hasOwnProperty(i)){
                    if(inlineCSS[i] != ""){
                        finalRules[i] = inlineCSS[i];
                    }
                }
            }

            //and voila!
            return finalRules;
        }

        var style = get_inheritance_result($("#fool"));
        console.log(style); //logs all the resulted css rules
        alert(style.marginLeft); //  auto !
        alert(style.fontSize); //  100px !
    });
</script>

www.un.org/Depts/DGACM/index_spanish.htm 由于提出了这一问题,学会了许多努力解决这一问题的经历。

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签