[英]Why my simple jQuery code doesn't work with .css() method?
I have the following simple code but it seem that not work. 我有以下简单的代码,但似乎不起作用。
jQuery(document).ready(
function($)
{
$(window).resize(
function()
{
setGrid($);
}
);
setGrid($);
}
);
function setGrid($)
{
var contactContainer = $('#contactInfo');
if(contactContainer.width() < 650)
{
console.log('Too short');
$('.col_1_2').css(
{
width : '100% !important',
margin : '0 !important'
}
);
}
else
{
console.log('Too long');
$('.col_1_2').css(
{
width : '49% !important',
marginRight : '1% !important'
}
);
$('.last').css(
{
width : '49% !important',
marginRight : '0 !important',
marginLeft : '1% !important'
}
);
}
}
So, when I resize the window I am getting both the messages in my Chrome console, according to the #containerInfo, but the .col_1_2 are not updated. 因此,当我调整窗口大小时,根据#containerInfo,我在Chrome控制台中同时收到了两条消息,但是.col_1_2并未更新。
Is there anything wrong in my code and I cannot find it out ? 我的代码有什么问题,我找不到吗?
Note : I have my console open and I do not get any error message in my console. 注意:我的控制台已打开,但控制台中未出现任何错误消息。 Also in the "Elements" tab of my console I am inspecting the element that must get modified, and I do not see any change.
同样在控制台的“元素”选项卡中,我正在检查必须修改的元素,但看不到任何更改。 Normaly a "style" attribute should be added on the matched element.
通常,应在匹配的元素上添加“样式”属性。
Kind regards 亲切的问候
尝试脱掉!important ...不需要它们,因为.css所应用的样式会直接添加到元素中(覆盖所有可能应用的其他样式)。
Try this, You are missing '(quote)
in attribute. 试试这个,您在属性中缺少
'(quote)
。 Add quote
in all css then it will work. 在所有CSS中添加
quote
,然后它将起作用。
$('.col_1_2').css(
{
'width' : '49% !important',
'margin-right' : '1% !important'
}
);
jQuery will not allow to set !important as you have tried. jQuery将不允许您尝试设置!important。 However, I think the following will work for you.
但是,我认为以下将为您工作。
jQuery.style(name, value, priority);
You can use it to get values with .style('name') just like .css('name'), get the CSSStyleDeclaration with .style(), and also set values - with the ability to specify the priority as 'important'. 您可以使用它来获取.style('name')的值,就像.css('name')一样,使用.style()获取CSSStyleDeclaration,还可以设置值-可以将优先级指定为“重要” 。 See https://developer.mozilla.org/en/DOM/CSSStyleDeclaration .
参见https://developer.mozilla.org/en/DOM/CSSStyleDeclaration 。
Try something like: 尝试类似:
var div = $('someDiv');
console.log(div.style('color'));
div.style('color', 'red');
console.log(div.style('color'));
div.style('color', 'blue', 'important');
console.log(div.style('color'));
console.log(div.style().getPropertyPriority('color'));
However, using media query is the correct way to proceed. 但是,使用媒体查询是继续进行的正确方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.