English 中文(简体)
Embed a JQuery expression inside a css file?
原标题:

As the question implies, can I add a JQuery script inside an css file as an expression? Or something like:

.parent{
    background-color: pink;
    position:relative;
    width: 100%;
}

.child{
    position: absolute;
    background: transparent url( ./background.gif ) no-repeat top right;
    /*height: 100%;*/
    height:  expression($(this).parent.height()+ px );
    width: 12px;

}

the whole idea of this script is to solve the 100% div height in IE6.

问题回答

You cannot do it that way.

You could perform some kind of javascript when the page loads... (A function that looks for elements with the 100% height and replace that with the parent s height.)

Not on any browser I tried; There are CSS expressions that might work on IE 6, but they re not exactly speedy (so says the link). I think the problem is when the CSS loads vs when the jQuery selector is initialized. So you ll have to write some on DOM ready code to select all of the elements that need their heights set and then set it.

#element {
    height: expression((function(element) {
              var x = window.setInterval (function () {
                    if (document.readyState.search (/loaded|complete/) > -1) {
                      window.clearTimeout (x);
                      $(element).height($(element).parent.height())
                    }
                  }, 50);
            })(this));
}

Let me explain: Firstly, expressions are bad. Follow dlablin s link to read why. You should consider other methods. If you must, the above snippet works as follows:

  1. The expression is first evaluated as soon as the CSS is read. That means, the DOM is not yet ready and jQuery is probably not yet loaded.

  2. Set as expression an anonymous function, that is instantaneously executed.

  3. This function delivers the current element as element to the containing code.

  4. We set an interval: Check every 50ms, if the DOM is ready. If it is, clear the interval and run the jQuery.

Remark: If you do this with lots of elements, this will really slow down your page. To emphasize this again: Don t do this at home, kids!





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签