Javascript 单击图像以显示内容

Javascript 单击图像以显示内容,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我已经找到了这个问题的答案,但似乎找不到适合我的答案。我想要的是一个图像,它在页面上呈现为一个普通的标签。我希望该图像是可点击的,当它被点击时,一些内容将下降到它下面的图像描述,等等。然后,当图像再次被点击时,它将隐藏内容 我的(初学者)javascript尝试如下所示: <script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"> $(window).load(function(){ $(".hea

我已经找到了这个问题的答案,但似乎找不到适合我的答案。我想要的是一个图像,它在页面上呈现为一个普通的标签。我希望该图像是可点击的,当它被点击时,一些内容将下降到它下面的图像描述,等等。然后,当图像再次被点击时,它将隐藏内容

我的(初学者)javascript尝试如下所示:

<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js">

$(window).load(function(){
$(".header").click(function () {

$header = $(this);
//getting the next element
$content = $header.next();
//open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
$content.slideToggle(500, function () {
    //execute this after slideToggle is done
    //change text of header based on visibility of content div
    $header.text(function () {
        //change text based on condition
        return $content.is(":visible") ? "Collapse" : "Expand";
    });
});  

$(窗口)。加载(函数(){
$(“.header”)。单击(函数(){
$header=$(此);
//获取下一个元素
$content=$header.next();
//打开所需内容-切换幻灯片-如果可见,向上滑动,如果不向下滑动。
$content.slideToggle(500,函数(){
//滑动切换完成后执行此操作
//根据内容div的可见性更改标题文本
$header.text(函数(){
//根据条件更改文本
返回$content.is(“:visible”)?“Collapse”:“Expand”;
});
});  

HTML是这样的:

    <div class="container">
<div class="header"><img src="Placeholders/placeholder.jpg" width="500" height="333"></div>
<div class="content" style="display: none;">
    The words should be here.
</div>

文字应该在这里。


如果您有任何帮助,我将不胜感激。如果在别处得到了答复,我深表歉意。我找不到它。

您的代码需要一个单独的
脚本标记。您不能重复使用导入jQuery的标记

<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
  // Your code
</script>

//你的代码
试试这个html:

 <div class="container">
    <div class="header"><img src="Placeholders/placeholder.jpg" width="500" height="333"></div>
    <div class="content" style="height: 0px;overflow:hidden;">
        The words should be here.
    </div>

在JSFIDLE上:

我已经有了这个,但我一定忘了在原始帖子中包含它。我为混淆道歉。这段代码工作得很好。我对代码了解不多,所以我感谢您花额外的精力在这里发布您的代码。
$(document).ready(function(){
    $(".header").click(function () {

        if($(".content").height() === 0){
             $(".content").animate({height: "20px"}, 500);
        }else{
            $(".content").animate({height: "0px"}, 500);
        }

    });
})