jquery通过选择值来设置div

时间:2016-11-24 09:45:53

标签: javascript jquery css

<div width="30%" height="60%" class="data-list aws-prio-h" style="cursor:pointer;" value="3087"></div>
<div width="30%" height="60%" class="data-list aws-prio-h" style="cursor:pointer;" value="3088"></div>
<div width="30%" height="60%" class="data-list aws-prio-h" style="cursor:pointer;" value="3088"></div>

如何使用value =“3087”

更改上面div的背景颜色

我尝试了以下jquery代码

$(".data-list aws-prio-h value["3087"] ").css("background-color","red");

不工作

4 个答案:

答案 0 :(得分:3)

  • 缺少.,因为它是class selector
  • 属性选择器错误,必须包含在[] - &gt; [Attribute = 'Value']
  • 删除选择器之间的空间,因为它只是元素,没有父子层次结构

&#13;
&#13;
$(".data-list.aws-prio-h[value='3087']").css("background-color", "red");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div width="30%" height="60%" class="data-list aws-prio-h" style="cursor:pointer;" value="3087">A</div>
<div width="30%" height="60%" class="data-list aws-prio-h" style="cursor:pointer;" value="3088">B</div>
<div width="30%" height="60%" class="data-list aws-prio-h" style="cursor:pointer;" value="3088">C</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

jQuery上有CSS选择器错误。试试这个:

log="cd**  /opt/logs"

这将完美无缺。

答案 2 :(得分:0)

检查以下代码

&#13;
&#13;
$( document ).ready(function() {
    $('.data-list.aws-prio-h[value="3087"]').css("background-color","red");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div width="30%" height="60%" class="data-list aws-prio-h" style="cursor:pointer;" value="3087">trest</div>
<div width="30%" height="60%" class="data-list aws-prio-h" style="cursor:pointer;" value="3088">xgdsfg</div>
<div width="30%" height="60%" class="data-list aws-prio-h" style="cursor:pointer;" value="3088">dsgdg</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

$('.data-list[value="3087"]').css('background-color', 'red');