fadeToggle 在静态 HTML 元素上工作正常: HTML:
<div id="wrapper" style="
position: relative;
display: inline-block;
width: 116px;
height: 116px;
background: url('http://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Example.jpg/116px-Example.jpg') no-repeat;
">
<img id="cover" src="http://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Example.png/116px-Example.png" />
</div>
查询:
$("#wrapper").click(function() {
$("#cover").fadeToggle("slow");
});
http://jsfiddle.net/BishKopt/zFq5P/1/
但是当我尝试用 JQuery 添加包装器时......
HTML:
<img id="cover" src="http://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Example.png/116px-Example.png" />
查询:
$wrapper = $('<div></div>');
$wrapper.attr('id', 'mywrapper');
$wrapper.css({
position: "relative",
display: "inline-block",
width: $("#cover").width(),
height: $("#cover").height(),
background: "url('http://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Example.jpg/116px-Example.jpg') no-repeat"
});
$("#cover").wrap($wrapper);
$("#mywrapper").click(function() {
$("#cover").fadeToggle("slow");
});
http://jsfiddle.net/BishKopt/kazKF/1/
...在第二次切换(第二次单击)之后 - img html 元素被“选中”...您有什么想法可以避免它吗?谢谢!