2014-01-09 55 views
0

我正在使用增加和減小縮放圖像,但我想顯示 當用戶單擊圖像時減少。當我嘗試這個代碼時;減少不起作用 ..爲什麼?jquery增加和減少zomming不起作用

<script type='text/javascript'> 
$(window).load(function(){ 
    var cell = $('#pok'); 
    $('.increase').click(function(){ 
     cell.height(cell.height()+20); 
     $(".title").html('<a href="#" class="decrease">Decrease</a>') 
    }); 
    $('.decrease').click(function(){ 
     cell.height(cell.height()-20); 
    }); 
}); 
</script> 
<img src="/41.jpg"id="pok"class="increase"><span class="title"></span> 

回答

0

使用on()dynamically created elements像更換.click

$(document).on('click','.decrease',function() { 
    cell.height(cell.height() - 20); 
}); 

還利用$(function(){代替$(window).load(){看到difference

Working Demo

+0

謝謝verymuch – user3172316

0

.on

$(window).load(function(){ 
var cell = $('#pok'); 
$('.increase').click(function(){ 
    cell.height(cell.height()+20); 
$(".title").html('<a href="#" class="decrease">Decrease</a>') 
}); 
$('.decrease').on("click",function(){ 
    cell.height(cell.height()-20); 
}); 

}); 

文檔從on()功能

This method is a shortcut for .bind('click', handler) 
+0

應該是'上(‘點擊’,' –

+0

謝謝你的回答,但我試着不工作 – user3172316

+0

@ user3172316請檢查更新的答案 –

0

你所面臨的問題是,當你選擇與類.decrease你得到0元素,因爲你重視它只是當你點擊的元素增加。

改變「.decrease」選擇爲「.title僞」,也將努力

0

試試這個:

<script type='text/javascript'> 
    jQuery(document).ready(function($){ 
     $('.increase').click(function(){ 
      var cell = $(this); 
      cell.height(cell.height()+20); 
      $(this).find(".title").html('<a href="#" class="decrease">Decrease</a>'); 
     }); 
     $('.decrease').click(function(){ 
      var cell = $(this).parent().find('.increase'); 
      cell.height(cell.height()-20); 
     }); 
    }); 
</script> 

<img src="/41.jpg"id="pok"class="increase"><span class="title"></span> 
+0

謝謝verymuch – user3172316

+0

如果你發現答案被接受,請將它標記爲已接受... –