javascript - 隐藏 div 也会隐藏父 div 的图像

标签 javascript jquery html css

我试图将一个名为 chooseArena 的菜单隐藏在一个名为 header 的较大 div 中。当我隐藏 chooseArena 时,它也会隐藏较大 div 的图像。我该怎么做才能只隐藏较小的 div 而不是 larder div 的图像。

这是我的 html jquery

<html>
<head>
    <title>Dojo Arena</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript"  src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('button').hover(function(){
            var arena = $(this).attr('id');
            var id = arena + '.jpg';
            $('#header').css('background-image', 'url('+id+')');
        }, function(){
            $('#header').css('background-image', 'none');
        });
        $('button').click(function(){
            var arena = $(this).attr('id');
            var id = arena + '.jpg';
            $('#header').css('background-image', 'url('+id+')');
            $('#chooseArena').hide();  // This also hides the image above.  What do I change so it will only hide the div and not the parent div with the background img
        });
    });
    </script>
</head>
<body>
    <div id="header">
        <div id = "chooseArena">
            <h1>Choose an Arena</h1>
            <button id = "dojo">Dojo</button>
            <button id = "beach">Beach</button>
            <button id = "earth">Earth</button>
            <button id = "matrix">Matrix</button>
            <button id = "snow">Snow</button>
            <button id = "forest">Forest</button>
        </div>
    </div>
</body>
</html>

这是我的CSS文件

*{
    background-color: black;
    margin: 0;
    padding: 0;
}

#chooseArena{
    border: 1px solid: red;
    margin-left: 40%;
    width: 20%;
    height: 200px;
    background-color: grey;
    margin-top: 20%;
}

button{
    background-color: white;
    width: 30%;
    margin-left: 7px;
    margin-bottom: 20px;
}

h1{
    background-color: grey;
    margin-left: 20%;
}

#header{
    height: 900px;
    background-repeat: no-repeat;
    width: 100%;
    height: 100%;
    background-size: 100% 100%;
    padding-top: 5%;
}

option{
    background-color: white;
}

select{
    background-color: white;
}

最佳答案

试试这个:

 $('#chooseArena').css("opacity","0");

我的猜测是隐藏 div,这意味着将其设置为“显示:无”会将标题高度和宽度设置为 auto,因此您看不到背景图像。

关于javascript - 隐藏 div 也会隐藏父 div 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41769776/

相关文章:

javascript - 类型错误 : url is not a function

javascript - Bootstrap 轮播在鼠标悬停或单击之前不会滑动

javascript - 单击事件仅在元素放置在标题中时触发

html - 为什么html表格有多个列的数据到一列

javascript - 如何用 jQuery 中的 dataTables 替换标准引导表?

javascript - Node : Passing function specific variables to Async. 并行()

jquery - 如何在数据表中包含 tfoot

javascript - 在 javascript 或 jquery 中禁用日期选择器的下一个日期

html - <td> 中的内容重叠

javascript - 在 Ext-JS 4 中的 this.control() 之前将参数传递给函数