我正在努力将一些可变值放入 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/