如何在zIndex中添加“重要”

时间:2015-05-31 09:39:26

标签: javascript css z-index

我网站上的某些加载项之间存在冲突。使用Firebug,我注意到如果我在z-index中添加“!important”,我可以避免冲突。

但是z-index值是使用JavaScript而不是CSS

设置的

所以如何在以下JS代码中添加“!important”:

this.menu = $("<ul>")
   .addClass("ui-autocomplete")
   .appendTo(this.document.find(this.options.appendTo || "body")[0])
   .zIndex(this.element.zIndex() + 1)  // !! <- here // !!
   .hide()
   .data("menu");

编辑:从提议的答案[How to apply !important using .css()?]我明白我需要添加以下代码 -

if (XXX.style.setProperty) {  // IE 9- does not support...
    XXX.style.setProperty ("z-index", (this???.element.zIndex() + 1), "important");
  }

如何添加:我的情况下XXX是什么?和“这个”?

编辑2:因为它成了jQuery语法问题,我在 - How to add 'if' to jQuery chain code

中用不同的标签问了它

4 个答案:

答案 0 :(得分:1)

我想我得到了这个

this.menu = $("<ul>")
   .addClass("ui-autocomplete")
   .appendTo(this.document.find(this.options.appendTo || "body")[0])
//   .zIndex(this.element.zIndex() + 1)  // !! <- here // !!
   .style.setProperty ("zIndex", zIndex()+1, "important");
   .hide()
   .data("menu");

答案 1 :(得分:1)

我在另一个帖子中收到了答案 - How to add 'if' to jQuery chain code

使用 -

.each(function() {
    this.style.cssText+= 'z-index: '+(parseInt($(this).css('z-index'))+1)+' !important';
  })

但是在使用了几个函数中的代码之后,我更喜欢在这个类中添加一个类和一个规则 -

this.menu = $("<ul>")
  .addClass("ui-autocomplete")
  .addClass("ui-autocomplete-hover-header")  // !! added!
. . .

.ui-autocomplete.ui-autocomplete-hover-header {  
  z-index: 5004 !important; }

答案 2 :(得分:0)

为什么不使用css呢?例如

long long factors(int N) {
    int nr = (int) sqrt(N);
    int b;

    int fix = N - nr * nr - 1;
    long long ans = 0;
    for (b = 2; b <= nr; b++) {
        if (N % b == 0) {
            ans += N/b - 1;
        } else {
            ans += N/b;
        }
    }

    if ( fix < 0) {
        ans = (2*ans) + N + fix;
    } else {
        ans = (2*ans) + N + fix - 1;
    }

    return ans; 
}

然后定义this.menu = $('<ul class="high-zindex">')....

答案 3 :(得分:-2)

试试这个。

.zIndex(this.element.zIndex() + 1 + '!important')