【问题标题】:Jquery if <div> style contains rgbJquery 如果 <div> 样式包含 rgb
【发布时间】:2018-08-14 17:14:52
【问题描述】:

我想检查元素的样式是否有 rgb。例如

<div class="grid" style="background-color: rgba(116, 185, 255, 0.5)">

jquery:

if $('.grid') style has rgba {
    //do it
}

【问题讨论】:

  • 你的意思是它是否有颜色?或者如果颜色是 rgba?
  • 如果颜色是 rgba

标签: jquery styles element


【解决方案1】:

希望对你有帮助。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>css demo</title>
  <style>
  div {
    width: 60px;
    height: 60px;
    margin: 5px;
    float: left;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<span id="result">&nbsp;</span>
<div style="background-color: blue; color: red">empty</div>
<div style="background-color: rgba(0,1,0,1); color: red"></div>
<div style="background-color:#123456;"></div>
<div style="background-color:#f11;"></div>

<script>
$( "div" ).click(function() {
  var bodyColor = $(this).attr("style");

  if (bodyColor.indexOf('rgba') > -1){
  	$( "#result" ).html( "That div <span style='" +
    bodyColor + ";'>has RGBA</span>." );
  }else{
  	$( "#result" ).html( "That div <span style='" +
    bodyColor + ";'>does not have RGBA</span>." );
  }
});
</script>
</body>
</html>

【讨论】:

  • 谢谢。你的 sn-p 工作正常。但我不明白为什么我的代码不能正常工作。你能检查一下吗? jsfiddle.net/xcqpF/1554
【解决方案2】:

使用它:)

var element = document.getElementsByClassName('grid');
if(element){
  var style = element.style;
  if(style){
    var color = style.color;//or backgroundColor
    if(color){
      if(color.indexOf(rgba) > -1){
        //Do whatever you want
      }
    }
  }
}

在 jQuery 中:

if ($(".words").eq(0).css("color").indexOf("rgba") > -1){
  //Do whatever you want
}

【讨论】:

  • 你能把它转换成jquery吗?我无法集成到我的 juqery 代码中。
猜你喜欢
  • 2023-03-16
  • 2015-12-03
  • 2012-02-24
  • 1970-01-01
  • 2013-01-08
  • 2013-10-27
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多