简体   繁体   English

内联如果在 Javascript 中操作 CSS 代码

[英]Inline If in Javascript to manipulate CSS code

Im trying to hide the calendar button if the page width is smaller than X pixels.如果页面宽度小于 X 像素,我试图隐藏日历按钮。 There seems to be an incorrect syntax issue but I cant find out what I am doing wrong.似乎存在不正确的语法问题,但我无法找出我做错了什么。 Here is the code;这是代码;

'calendars' : {
                    label : Dictionary.Calendar.AddCalendars,
                    icon : 'fa-calendar',
                    showText : false,
                    css : (($('.ak-page').width() < 500px ) ? 'col-xs-hide' : ''),

there are more properties below css, Im not going to include them here because I know the problem is at the line where I define 'css' property.在 css 下面还有更多的属性,我不会在这里包含它们,因为我知道问题出在我定义 'css' 属性的那一行。

500px is invalid JavaScript syntax. 500px是无效的 JavaScript 语法。

Since jQuery's width() method returns the number of pixels as an integer, I think you mean:由于 jQuery 的width()方法以整数形式返回像素数,我想你的意思是:

css : (($('.ak-page').width() < 500 ) ? 'col-xs-hide' : '')

Or, if you can support window.matchMedia , you can achieve this more reliably by checking the width of the screen, not the width of an arbitary element in your DOM:或者,如果您可以支持window.matchMedia ,您可以通过检查屏幕的宽度而不是 DOM 中任意元素的宽度来更可靠地实现这一点:

css : (window.matchMedia('(max-width: 499px)').matches ? 'col-xs-hide' : '')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM