div上的onblur无法用于内部输入子元素

时间:2018-10-31 08:01:50

标签: javascript html

我已将onblur应用于div,并希望在其子元素失去焦点时触发该事件。 div有两个子元素,其中一个是文本框,另一个是div。现在,当我单击文本框并在父div外部单击时,它不会触发onblur事件,但是如果我在子div上单击并在父div外部单击,那么它将触发该事件。

我的要求是,如果我单击文本框并单击其他div,则父div的onblur事件也应触发,就像在其他子元素的情况下触发一样。我在此处包括我的代码段,您可以运行以查看输出。如果您想在jsfiddle中查看代码,然后在click here中查看。请帮忙。

function searchOnClick() {
        document.getElementById("resultsSugg").className = "position-absolute d-block";
    }

    function focusOut() {
        document.getElementById("resultsSugg").className = "position-absolute d-none";
    }

    function resOnclick(that) {
        let data = that.getAttribute("data-ppp");
        document.getElementById("inputText1").value = data;
    }
.suggestions {
  font-size: 0.9em;
  border-bottom: 1px solid #eeeeee;
  padding: 0.5rem 0.5rem;
  background-color: white;
}

.suggestions:hover {
  background-color: #f4f4f4;
}

.suggestions:last-child {
  border-bottom: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-4">
            <label>dummy text</label>
        </div>
        <div class="col-4">
            <div id="bloodhound">
                <!--<input type="text" class="typeahead form-control" id="typeAheadText">-->

                <div tabindex="0" onblur="focusOut()">

                    <input type="text" class="typeahead form-control dropdown-toggle"
                           id="inputText1" onkeyup="return searchElastic()" onclick="searchOnClick()"/>

                    <div class="position-absolute d-none" style="width: 100%; border:1px solid #eeeeee; z-index: 999;"
                         id="resultsSugg">

                        <div class="suggestions" onclick='return resOnclick(this)' data-ppp='John'>
                            <span>John</span> <span>Doe</span>
                        </div>
                        <div class="suggestions" onclick='return resOnclick(this)' data-ppp='Rocky'>
                            <span>Rocky</span> <span>Balboa</span>
                        </div>
                        <div class="suggestions" onclick='return resOnclick(this)' data-ppp='Samim'>
                            <span>Samim</span> <span>Yo</span>
                        </div>

                    </div>

                </div>
            </div>
        </div>
        <div class="col-4">
            <label>dummy text</label>
        </div>
    </div>
    <div class="row">
        <div class="col-10 offset-1">
            <div>
                <br><br><br>
                <strong>Steps to reproduce the problem</strong><br>
                <ul>
                    <li>Click on the textbox, then</li>
                    <li>Click on dummy text and click just outside of the parent div. It won't hide</li>
                </ul>
            </div>
        </div>
    </div>

</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

</body>
</html>

0 个答案:

没有答案