Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery在windows宽度小于值时更改元素的属性_Jquery_Css - Fatal编程技术网

使用jquery在windows宽度小于值时更改元素的属性

使用jquery在windows宽度小于值时更改元素的属性,jquery,css,Jquery,Css,我想当windows宽度小于992时,更改一个元素的css属性。我写了这段代码: $(document).ready() { if ($(window).width() < 992) { $('#header_right').css('text-align', 'center'); } } $(document).ready() { 如果($(窗口).width()

我想当windows宽度小于992时,更改一个元素的css属性。我写了这段代码:

$(document).ready()
{
    if ($(window).width() < 992) {
        $('#header_right').css('text-align', 'center');
    }
}
$(document).ready()
{
如果($(窗口).width()<992){
$('#标题_right').css('text-align','center');
}
}
如果我执行

if ($(window).width() < 992) {
    $('#header_right').css('text-align', 'center');
}
if($(窗口).width()<992){
$('#标题_right').css('text-align','center');
}
在控制台中由ff完成,但在文件中使用此代码时,如up不起作用

我在head中使用此js文件:

<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/main.js"></script>


请提供建议。

这最好通过
@media
查询来解决。您需要在CSS中添加以下内容:

@媒体屏幕和(最大宽度:992){
#标题(右){
文本对齐:居中;
}
}
您必须在
文档的
就绪
函数中使用
窗口
调整大小
事件将其绑定。实际上,您有一个语法错误:

$(document).ready(function () {
  $(window).resize(function () {
    if ($(window).width() < 992) {
      $('#header_right').css('text-align', 'center');
    }
  });
});
$(文档).ready(函数(){
$(窗口)。调整大小(函数(){
如果($(窗口).width()<992){
$('#标题_right').css('text-align','center');
}
});
});

因为您当前的代码在加载文档时只检查一次,而不是每次调整窗口大小时都检查一次。

控制台中是否有错误?是的,是否有错误。您可以使用当前运行吗?您的代码也不会在
jQuery.ready()
中运行。它在调用
ready
之后运行,这很可能是在文档准备好之前。css代码不起作用,但js代码目前起作用。谢谢你,问题解决了。