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

jquery draggable and changing the color of the background container

本文关键字:颜色 背景 拖动 jquery      更新时间:2023-09-26

我遇到语法错误,因为我显然做错了什么。

这是代码:

 <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开发工具中,语法错误如下:

#容器("背景色:绿色");

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

谨致问候,umbre gachoong

更换

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

带有

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