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);
为什么它不起作用?我有点过时了......
答案 0 :(得分:2)
代码在结构上是正确的,问题是你犯了两个拼写错误。
opacity
取0到1之间的值,而不是百分比。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 :(得分:2)
将值更改为0到1
var imgadd = $("<img/>",{
src:"http://idordt.nl/wp-content/uploads/2014/06/wk-koala.jpg",
alt:"Koala",
id:"koala",
click:function(){alert("hello");$(this).css("opacity","0.2");},
mouseenter:function(){$(this).css("height","200px")}
})
$("body").append(imgadd);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;