jquery可拖动并更改背景容器的颜色

时间:2014-04-24 17:57:22

标签: javascript jquery css jquery-ui

我收到语法错误,因为显然我做错了。

以下是代码:

 <style>
    body{margin:0 auto;}
    .draggable { width: 100px; height: 100px; padding: 0.5em;background-color:red;}     

    </style>
    <script>
    $(function() {
        $( ".draggable" ).draggable();
    });
    var i=0;
    $(".draggable").each(function(i){
     if(i%2==0){
        #container("background-color:green");
       }
       i++;
       });
    </script>
 </head>
<body>

<div id="container" style="width:100%;height:400px;background-      color:#ccc;position:relative;">
<div class="draggable" class="ui-widget-content" style="margin-top:10%;margin-    left:1%;position:absolute;">
    <p>Drag me around</p>
</div>
<div class="draggable" class="ui-widget-content" style="margin-top:10%;margin-    left:20%;position:absolute;">
    <p>Drag me around</p>
</div>
<div class="draggable" class="ui-widget-content"style="margin-top:10%;margin-    left:33%;position:absolute;" >
    <p>Drag me around</p>
</div>
<div class="draggable" class="ui-widget-content"style="margin-top:10%;margin-    left:45%;position:absolute;" >
    <p>Drag me around</p>
</div>
<div class="draggable" class="ui-widget-content"style="margin-top:10%;margin-    left:60%;position:absolute;" >
    <p>Drag me around</p>
</div>
<div class="draggable" class="ui-widget-content" style="margin-top:10%;margin-    left:80%;position:absolute;">
    <p>Drag me around</p>
</div>
</div>

在Web开发人员工具中,语法错误在于:

#container的( “背景色:绿色”);

我需要做的是当可拖动的div移出容器时,如果容器中剩余的可拖动div的数量为奇数,则容器的背景颜色变为绿色。

此致 umbre gachoong

1 个答案:

答案 0 :(得分:2)

替换

#container("background-color:green");

$("#container").css("background-color", "green");