1

私はこれをスタックオーバーフロー全体で検索しましたが、多くの回答が見つかりましたが、私が特に目指しているものでは機能しません...基本的に、このコードが機能しない理由がわかりません誰か助けてください! ご意見をお寄せいただきありがとうございます。

これは HTML で、divクリックするidとアニメーションの高さで表示されますelementTopdivelementBottom

<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'});
  }
}
4

2 に答える 2

5

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-13T15:30:14.360 に答える
1

jQuery を使用していて、使用していない場合があるのは少し奇妙です。使用する

var e = $('#' + id);

操作したい Dom 要素を取得します。それからあなたはただすることができます

e.animate();
于 2013-08-13T15:32:06.203 に答える