javascript - jquery 中的不同视口(viewport)宽度/高度插入

标签 javascript jquery css

我正在努力将一些可变值放入 jquery 选择器中。

// this works below
    $(".portrait").css({
        "width": viewportwidth + 'px',
        "height": viewportheight + 'px'
    });

但我不得不放弃上面的 jquery .css 选项,而是使用 .attr,因为我需要将 !important 选择器应用于我的宽度和高度。

我实现此目的的唯一方法是使用 .attr(我认为)

// this works below but I can only put a hardcoded value in
    $('.portrait').attr('style', 'width: 0px !important;');


// this is my attempt but fails
    $('.portrait').attr('style', 'width: viewportwidth + 'px' !important;');


// below is exactly what I ideally need to work, multiple styles with !important
    $('.portrait').attr({
        'style', 'width: viewportwidth + 'px' !important;'
        'style', 'height: viewportheight + 'px' !important;'
    });

// but it does not work cause of my bad coding.

如果有人可以就变通或修复提出建议,那将非常有帮助。如果您需要我更详细地解释,请告诉我。

提前致谢:-)

乔希

最佳答案

尝试以下操作:

$(".portrait").css({
        "width": viewportwidth + 'px !important',
        "height": viewportheight + 'px !important'
    });

http://jsfiddle.net/manseuk/5vCab/

.css() 样式/CSS 方法和 .attr() 用于属性(标题、src、alt 等)

关于javascript - jquery 中的不同视口(viewport)宽度/高度插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8079538/

相关文章:

javascript - Tokbox StreamCreated 被调用的次数与客户端被调用的次数相同

javascript - 类泛型可分配问题

javascript - 使用 Angular,如何在两个功能之间切换?

angularjs - 如何在 angular1.5 comps 中修复页脚

javascript - 将重复序列复制到 TypedArray 中的更有效方法?

javascript - 单击时不应对子项起作用

javascript - 根据窗口大小更改列数

javascript - 将元素附加到 jQuery 中的主体顶部

html - 是否可以在绝对 div 中 float 元素?

css - 在 selenium IDE 中查找字体粗细值