[英]Check if div has a specific style applied
On my website if the page is first loaded and it's maximized/fullscreen, the div comBrand
has the particular CSS properties I want applied. 在我的网站上,如果页面首先被加载且已最大化/全屏显示,则div
comBrand
具有要应用的特定CSS属性。
During a resize event I apply different properties to that element using the .css()
function so that the div doesn't overlap with other screen items, however, instead of actually changing the CSS it appears to add a style attribute to that div in the code. 在调整大小事件期间,我使用
.css()
函数将不同的属性应用于该元素,以使div与其他屏幕项不重叠,但是,实际上并未更改CSS,而是在该div中添加了一个style属性编码。 Sometimes depending on how you resize the page, the resize function might have ended in such a way that when you use the maximize button, you end up the incorrect placement of the particular div. 有时,取决于您调整页面大小的方式,调整大小功能可能会以某种方式结束,即当您使用最大化按钮时,您会结束特定div的不正确放置。 I am checking for a few things to make sure that doesn't happen.
我正在检查一些事情以确保不会发生。
However, I don't know if the syntax here is correct for checking the style attribute of a div during the resize function because it doesn't seem to work as expected. 但是,我不知道这里的语法在resize函数期间检查div的style属性是否正确,因为它似乎无法正常工作。
$(window).resize(function() {
//first check
if ( $('#comBrand').css('top') == '155px')
{
//second check to see if the peculiuar event was reached where the div is positioned incorrectly but the window isn't maximized
if ( ($('#comBrand').attr('style') == 'top: 155px;margin-left: -615px;left: 50%;') && (window.screen.width > window.screen.availWidth))
{
$('#comBrand').css({'top': '155px', 'margin-left': '-615px', 'left': '50%'});
}
else //assume that the page is already maximized and adjust the div accordingly
{
$('#comBrand').css({'top': '141px', 'margin-left': '0', 'left': '0'});
}
}
else //default else, if the CSS of the div is anything other than what was initially checked for, set it back to its default location
{
$('#comBrand').css({'top': '155px', 'margin-left': '-615px', 'left': '50%'});
}
});
I know it's a little confusing and somewhat of a hack but I'd still like to get it to function. 我知道这有点令人困惑,有些骇人听闻,但我仍然希望它能够正常运行。
I added two classes, and gave the div comBrand the class of maximized hard coded in the html, and then used this code in the resize function, however, it doesn't work or do anything... 我添加了两个类,并为div comBrand提供了在html中最大化的硬编码类,然后在resize函数中使用了此代码,但是,它不起作用或没有任何作用...
$(window).resize(function() {
if ($('#comBrand').hasClass('maximized'))
{
$('#comBrand').removeClass('maximized');
$('#comBrand').addClass('minimized');
}
else {
$('#comBrand').removeClass('minimized');
$('#comBrand').addClass('maximized');
}
});
You should add a CSS class
instead of using CSS properties individually. 您应该添加CSS
class
而不是单独使用CSS属性。 And after that just check using .hasClass
to find if class added. 之后,只需使用
.hasClass
检查是否添加了类。
Read More here: 在这里阅读更多:
http://api.jquery.com/hasclass/ http://api.jquery.com/hasclass/
http://api.jquery.com/addclass/ http://api.jquery.com/addclass/
While Apul's answer is definitely correct, it doesn't answer the question asked. 尽管Apul的答案绝对正确,但它并未回答所提出的问题。 To do this, you could do the following:
为此,您可以执行以下操作:
var styles = $('#myDiv').attr('style').split(';');
var stylesKeyValue = {};
styles.forEach(function(value) {
stylesKeyValue[value.split(':')[0]] = value.split(':')[1];
});
This would work with something like: 这将与类似的东西一起工作:
<div id="myDiv" style="background-color:pink;min-height:20px;"></div>
and then you could test values with: 然后您可以使用以下方法测试值:
console.log(stylesKeyValue['background-color'] == 'pink');
As Apul Gupta pointed out, you should be adding and removing classes. 正如Apul Gupta指出的那样,您应该添加和删除类。 So, set up a couple classes:
因此,设置几个类:
CSS: CSS:
.FirstClass
{
top: 155px;
margin-left -615px;
left:50%;
}
.SecondClass
{
top:141px;
margin-left:0;
left:0;
}
Next, change your logic to this: 接下来,将您的逻辑更改为:
if (!$('#comBrand').hasClass('FirstClass') && (window.screen.width > window.screen.availWidth)){
$('#comBrand').removeClass("FirstClass");
$("#comBrand").addClass("SecondClass");
}
}
else {
$("#comBrand").removeClass("SecondClass");
$("comBrand").addClass("FirstClass");
}
Your first nested if statement doesn't do anything...You were checking to see if the CSS was X - and if so, you were setting the CSS to X. So, I removed that code. 您的第一个嵌套if语句不执行任何操作...您正在检查CSS是否为X-如果是,则将CSS设置为X。因此,我删除了该代码。
I must admit your if/else logic is unoptimized(a bit redundant) and confusing, but this should give you an idea of what you need to do. 我必须承认您的if / else逻辑未经优化(有点多余)并且令人困惑,但这应该使您对需要做的事情有所了解。
I would look at this bit, as I'm not sure you can guarantee the format of that string. 我会看一下这一点,因为我不确定您是否可以保证该字符串的格式。 At the least, you should be comparing individual fields with
.css('')
. 至少,您应该将单个字段与
.css('')
进行比较。
$('#comBrand').attr('style') == 'top: 155px;margin-left: -615px;left: 50%;'
But as the others have suggested, I'd advocate using CSS classes instead of the style attribute. 但是正如其他人所建议的那样,我主张使用CSS类而不是style属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.