简体   繁体   English

设置div容器的高度会禁用div内的单击?

[英]setting height of div container disables click inside div?

This is weird. 真奇怪 I have an input button inside of 3 divs, the outer-most of which is id'ed as "container". 我在3个div内有一个输入按钮,其中最外面的ID被标识为“容器”。 When I set "container"'s height, no action is triggered when I click on the inputs id'ed as "switch" and "next". 设置“容器”的高度时,单击输入为“ switch”和“ next”的输入时,不会触发任何动作。 However, when I do not set a height for #container, the click actions do work. 但是,当我没有为#container设置高度时,单击操作会起作用。 What is causing this? 是什么原因造成的? Thank you! 谢谢! My html and css is below. 我的HTML和CSS在下面。

html html

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv:"Content-Type" content="text/html; charset=ISO-8859-1" />
    <script text="text/Javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/Javascript" src="jquery.js"> </script>

    <link href='http://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'>

    <link rel="stylesheet" type="text/css" href="jquery_home.css">


    <title> jquery home </title>


</head>
<body>

<div id="container"> 

    <h1 style="position: relative; top: 10%; width: 300px; margin-left: auto; margin-right: auto"> Learn jQuery </h1>

<div id="q_and_a_container" style="text-align: center">
    <div style="width: 200px; margin-left: auto; margin-right: auto; text-align: center" id="qa" height="500px"> Nate</div>
    <input type="submit" id="switch" value="See Answer" /> 
    <input type="submit" id="next" value="Next Question" />


</div>

<div id="answer_container">
    <textarea id="answer_here"> </textarea>
</div>

</div>

<div id="another_container" style="width: 300px; margin-left: auto; margin-right: auto; text-align: center">

    <h2 style="font-family: 'Chalk'">
<a href="add_question.html">Add question </a> </h2>

</div>





</body>

</html>

CSS 的CSS

@font-face {
    font-family: "Chalk";
    src: url("../../font/KGSecondChancesSketch.ttf");
}

h1 {
    font-family: "Chalk";
}

html, body {
    height: 100%;
    margin: 0;
}

#container {
    width: 100%;
    height: 50%;
}

body {
    background-image: url(../../../img/compressedbgs/chalkboard.jpg);
    background-size: 100% 100%;
    background-repeat: repeat-x;
}


#q_and_a_container {
    height: 50%;
    position: relative;
    float: left;
    top: 25%;
    left: 2%;
    width: 45%;
    margin: 1%;
    background-color: rgba(255, 255, 255, 0.1);

    -webkit-box-shadow: 0px 0px 12px rgba(0,0,0,0.4);
    -moz-box-shadow: 0px 1px 6px rgba(23, 69, 88, .5);
    -webkit-border-radius: 12px;
    -moz-border-radius: 7px;
    border-radius: 7px;

    background: -webkit-gradient(linear, left top, left bottom,
        color-stop(0% white), color-stop(15%, white), 
            color-stop(100%, #D7E9F5));
    background: -moz-linear-gradient(top, white 0%, white 55%, #D5E4F3);
    z-index: 45;
}

div#qa {
    position: relative;
    top: 25%;
    height: 75%;
    z-index: 50;
}

#answer_container {
    position: relative;
    float: left;
    height: 50%;
    top: 25%;
    left: 2%;
    width: 45%;
    background-color: rgba(0,0,0,0.1);
    margin: 1%;
    border: none;
    -webkit-border-radius: 12px;
    -moz-border-radius: 7px;
    border-radius: 7px;
}

input {
    width: 107px;
    margin-left: auto;
    margin-right: auto;
    background-color: rgba(255, 255, 255, 0.1);
    -webkit-border-radius: 7px;
    -moz-border-radius: 7px;
    border-radius: 7px;
    font-family: 'Droid Sans Mono';
    z-index: 1000;
}

textarea {
    color: white;
    font-family: "Droid Sans Mono";
    width: 100%;
    height: 100%;
    border: none;
    -webkit-border-radius: 12px;
    -moz-border-radius: 7px;
    border-radius: 7px;
    background-color: rgba(0,0,0,0.1);
    -moz-appearance: none;
    font-size: medium;
}

textarea:focus {
    outline: none;
    border: none;
}

#add_question {
    clear: both;
}

a {
    text-decoration: none;

}

a:visited {
    color: black;
}

JS: JS:

$(document).ready(function() {

    var question_number = 1;
    var tempText;
    var qno;


    $(document).on("click", "#switch", function(event) {
        if ($("#switch").val() == "See Answer") {
            tempText = $("#qa").text();
            $("#qa").text("");
            qno = "" + question_number;
            qno = "#" + qno;

            $.ajax({
                url: "answers.html",
                datatype: "html",
                success: function(data) {
                    $("#qa").html($(data).filter(qno).text());
                },
                error: function(r) {
                    alert("whoops, error in ajax request");
                }
            }); // end AJAX request

            // change value
            $("#switch").val("See Question");
        } else {

            temptText = $("#qa").text();
            $("#qa").text("");
            qno = "" + question_number;
            qno = "#" + qno;

            $("#qa").text(tempText);            
            $("#switch").val("See Answer");
            }
    });

    $(document).on("click", "#next", function(event) {
        question_number = question_number + 1;
        qno = "" + question_number;
        qno = "#" + qno;

        $.ajax({
            url: "questions.html",
            datatype: "html",
            success: function(data) {
                $("#qa").text($(data).filter(qno).text());
            },
            error: function(r) {
                alert("whoops, error in ajax request for next question");
            }
        }); // end AJAX request
    });

    $("textarea").keydown(function(e) {
    if(e.keyCode === 9) { // tab was pressed
        // get caret position/selection
        var start = this.selectionStart;
        var end = this.selectionEnd;

        var $this = $(this);
        var value = $this.val();

        // set textarea value to: text before caret + tab + text after caret
        $this.val(value.substring(0, start)
                    + "\t"
                    + value.substring(end));

        // put caret at right position again (add one for the tab)
        this.selectionStart = this.selectionEnd = start + 1;

        // prevent the focus lose
        e.preventDefault();
    }
});


})

The reason is because the z-index for the div is positioning it on top of the inputs. 原因是因为div的z-index将其定位在输入的顶部。 You have set z-index on the inputs but because they are static elements it is ignored. 您已经在输入上设置了z-index,但是由于它们是静态元素,因此将其忽略。

Add position:relative; 添加position:relative; to the inputs and they will be brought to the front. 输入,它们将被带到最前面。

Your div#qa will overlap the button. 您的div#qa将与按钮重叠。 div#qa has z-index: 50; div#qa z-index: 50; . change it to -1 will make you button clickable. 将其更改为-1将使您的按钮可点击。

div#qa {
    height: 75%;
    position: relative;
    top: 25%;
    z-index: -1;
}

Check Fiddle here. 在这里检查小提琴。

It is happening because of the div (id="qa") before your 2 buttons. 这是由于2个按钮之前的div(id =“ qa”)导致的。 you have mentioned a width of 200px and height of 500px which is making it cover the buttons too. 您提到了200px的宽度和500px的高度,这也使其覆盖了按钮。 As well you have z-index of 50 for it, which is making it come on top of your buttons. 同样,您的z-index为50,这使其位于按钮上方。

There are 2 things you can do, 您可以做两件事,

Either make z-index to some value so that the div comes after button layer (In case you want the div to be of same size) 要么将z-index设置为某个值,以使div位于按钮层之后(如果您希望div具有相同的大小)

Ex - 前-

z-index : -50;

or you can decrease the size of div and top margin to be something which will make buttons to come after the div. 或者,您可以将div的大小和上边距减小,以使按钮在div之后。

Ex - 前-

top : 5%;
height : 200px;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM