我搜索了所有的堆栈溢出,找到了许多答案,但没有一个工作与我的具体目的.基本上,我不知道为什么这个代码不工作,请帮助!任何投入都非常感谢,谢谢!
下面是用div单击id of elementTop的HTML,用动画高度显示的div是elementBottom
<div id="elementTop" onclick="toggle_visibility('elementBottom');" >
<img id="thumb" src="images/davey1.png" />
<a>davey blair</a>
</div>
<div id="elementBottom">
<p><span style="font-weight: bold;">age: </span>29</p>
<p><span style="font-weight: bold;">hometown: </span>Charleston,SC</p>
<p><span style="font-weight: bold;">regular or goofy: </span>Regular</p>
<p><span style="font-weight: bold;">years shredding: </span>lifetime</p>
<p><span style="font-weight: bold;">other sponsors: </span>naish, chucktown</p>
<p><span style="font-weight: bold;">favorite trick: </span>switch mobe</p>
<p><span style="font-weight: bold;">favorite place to shred: </span>with my homies</p>
<p><span style="font-weight: bold;">music preference: </span>all music</p>
<p><span style="font-weight: bold;">paper or plastic: </span>hands</p>
<p><span style="font-weight: bold;">cat or dog: </span>dogs</p>
<p><span style="font-weight: bold;">other hobbies: </span>living life. work to live never live to work, live life.</p>
</div>下面是我使用的JavaScript/jQuery和我注释掉的JavaScript行,但是没有注释的jQuery不起作用。
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.height == 'auto') {
// e.style.height = '0px';
$('#' + e).animate({height:'0px'});
} else {
// e.style.height = 'auto';
$('#' + e).animate({height:'auto'});
}
}发布于 2013-08-13 15:30:14
这是可行的,但是您永远不能将它动画化,因为当元素是0px很高时,您不能单击它:
http://jsfiddle.net/neuroflux/UYZY2/23/
狙击!
function toggle_visibility(id) {
$(id).stop().animate({height:'toggle'}, 500);
}
$('.clicker').on('click', function() {
toggle_visibility('#' + $(this).attr('name'));
});发布于 2013-08-13 15:32:06
您使用的jQuery有时不使用,这有点奇怪。使用
var e = $('#' + id);以获得要操作的Dom元素。那你就可以
e.animate();https://stackoverflow.com/questions/18213046
复制相似问题