2014-04-24 45 views
2

我收到語法錯誤,因爲顯然我做錯了什麼。jquery可拖動和更改背景容器的顏色

下面是代碼:

<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

回答

2

更換

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

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

感謝。進行更改但容器背景顏色不變。我認爲這一定是我在這裏使用的邏輯。 – umbregachoong

+0

我把它放在這裏http://inetwebdesign.com/draggable_example2.html – umbregachoong

+0

移動到代碼的底部,就在主體結束之前。 – Enve