2015-09-10 33 views
0
var imgadd = $("<img/>",{ 
     src:"../img/Koala.jpg", 
     alt:"Koala", 
     id:"koala", 
     click:function(){$(this).css("opacity","50%");}, 
     mouseenter:function(){$(this).css("hight","200px")} 
    }) 
    $("body").append(imgadd); 

爲什麼它不工作?我有點pizzled ...jQuery事件處理程序不起作用?

+1

什麼不起作用? – guradio

+0

這是什麼庫? – MortenMoulder

+0

什麼是錯誤,發佈錯誤行:)...有助於非常清楚地瞭解所有開發人員的問題。 – vinodh

回答

2

的代碼在結構上是正確的,問題是,你做了兩個錯字的。

  1. opacity取值介於0和1之間,不是百分比。
  2. height拼錯了。

這裏的代碼,將工作:

var imgadd = $("<img/>",{ 
    src:"http://idordt.nl/wp-content/uploads/2014/06/wk-koala.jpg", 
    alt:"Koala", 
    id:"koala", 
    click:function(){$(this).css("opacity","0.5");}, 
    mouseenter:function(){$(this).css("height","200px")} 
}) 
$("body").append(imgadd); 

而且一個的jsfiddle:http://jsfiddle.net/jaredcrowe/3fvht8s2/

+1

這。這是唯一正確的答案。被抓住了。 – Abhitalks

+0

謝謝!我可憐的英語...... –

+0

不客氣 – mplungjan

2

將值更改爲0到1

var imgadd = $("<img/>",{ 
 
\t \t \t src:"http://idordt.nl/wp-content/uploads/2014/06/wk-koala.jpg", 
 
\t \t \t alt:"Koala", 
 
\t \t \t id:"koala", 
 
\t \t \t click:function(){alert("hello");$(this).css("opacity","0.2");}, 
 
\t \t \t mouseenter:function(){$(this).css("height","200px")} 
 
\t \t }) 
 
\t \t $("body").append(imgadd);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

+0

我會讓你實施我的改變:))) – mplungjan