简体   繁体   English

检查div是否应用了特定样式

[英]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属性。

这<div>即使 "margin: 0px;" 元素也有边距应用样式</div><div id="text_translate"><p>我首先创建了一个名为snake-container的&lt;div&gt; ,其中包含 500x500px 的分割图块。 然后我在 Javascript 中使用了一个 for 循环来放置一个 25x25px 的 20x20px &lt;div&gt;网格,背景为黑色。 然而,与其创建我想要的网格,不如创建一个 625 &lt;div&gt;的长列。 事实证明,每个&lt;div&gt;都有一个奇怪的边距,在.tiles &lt;div&gt;内从左到右一直延伸。 我已将所有内容设置为 0 px 边距,但这些奇怪的行状边距仍然存在。 为什么会这样?</p><p> 这是代码: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;html&gt; &lt;head&gt; &lt;title&gt;Snake&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="snake-container"&gt; &lt;div id="tiles"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;script&gt; for (let i = 0; i &lt; 625; i++) { const tile = document.createElement("div"); tile.classList.add("tile"); document.getElementById("tiles").appendChild(tile); } &lt;/script&gt; &lt;style&gt; * { margin: 0px; padding: 0px; } #snake-container { width: 100%; height: 100%; display: flex; justify-content: center; } #tiles { width: 500px; height: 500px; }.tile { width: 20px; height: 20px; background-color: black; margin: 0px; } &lt;/style&gt; &lt;/html&gt;</pre></div></div><p></p></div> - The <div> element has a margin even though the "margin: 0px;" style is applied

暂无
暂无

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

相关问题 Javascript / AngularJS:检查元素是否具有应用样式y的样式 - Javascript/AngularJS: Check if an element has the style overflow-y applied to it jQuery:检查元素是否具有特定样式(非内联) - jQuery: check if element has specific style (not inline) 如何检查div在JavaScript中是否具有特定值 - How to check if div has a specific value in JavaScript 这<div>即使 "margin: 0px;" 元素也有边距应用样式</div><div id="text_translate"><p>我首先创建了一个名为snake-container的&lt;div&gt; ,其中包含 500x500px 的分割图块。 然后我在 Javascript 中使用了一个 for 循环来放置一个 25x25px 的 20x20px &lt;div&gt;网格,背景为黑色。 然而,与其创建我想要的网格,不如创建一个 625 &lt;div&gt;的长列。 事实证明,每个&lt;div&gt;都有一个奇怪的边距,在.tiles &lt;div&gt;内从左到右一直延伸。 我已将所有内容设置为 0 px 边距,但这些奇怪的行状边距仍然存在。 为什么会这样?</p><p> 这是代码: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;html&gt; &lt;head&gt; &lt;title&gt;Snake&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="snake-container"&gt; &lt;div id="tiles"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;script&gt; for (let i = 0; i &lt; 625; i++) { const tile = document.createElement("div"); tile.classList.add("tile"); document.getElementById("tiles").appendChild(tile); } &lt;/script&gt; &lt;style&gt; * { margin: 0px; padding: 0px; } #snake-container { width: 100%; height: 100%; display: flex; justify-content: center; } #tiles { width: 500px; height: 500px; }.tile { width: 20px; height: 20px; background-color: black; margin: 0px; } &lt;/style&gt; &lt;/html&gt;</pre></div></div><p></p></div> - The <div> element has a margin even though the "margin: 0px;" style is applied 如果 div 具有特定样式,jquery/js 隐藏提交按钮 - jquery/js hide submit button if div has specific style jQuery检查元素是否具有特定的样式属性(显示) - jQuery check if element has a specific style property (display) 使用getelementbyid时未应用Div样式 - Div style not being applied when using getelementbyid 检查每个div是否包含一个包含特定类的元素 - Check if each div has an element with a specific class inside it 用ajax添加后检查div是否具有特定文本 - Check if div has specific text after been added with ajax 为什么样式不适用于特定类的子节点? - Why is the style not applied to the child nodes of a specific class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM