简体   繁体   English

使用javascript将div框添加到DOM(html正文)后,将其关闭

[英]Closing a div box after it has been added to the DOM (html body) using javascript

I was hoping to get some help. 我希望得到一些帮助。 I set up a JSfiddle below: 我在下面设置了一个JSfiddle:

http://jsfiddle.net/y7mEY/102/ http://jsfiddle.net/y7mEY/102/

I was looking to have the grey div box that appears when the input box is clicked (that will have the search options in it) to close when I click anywhere outside it. 我一直希望单击输入框时会出现灰色的div框(它将在其中包含搜索选项),当我在其外部的任何位置单击时,它们会关闭。

I would like to not load jquery and therefore keep everything in javascript. 我不想加载jquery,因此将所有内容保留在javascript中。

Any help? 有什么帮助吗?

code below: 下面的代码:

HTML 的HTML

<div id = "searchHousing">
    <input type="text" name="value" id="fillIn"> 
</div>

CSS 的CSS

.searchBox {

    position: absolute; 
    font-family:Arial;
    font-size: 10pt;
    color:black;
    height: 200px;
    width: 200px;
    padding-left: 3px;
    padding-top: 1px;
    border: 1px solid white;
    background-color: grey;

}

#fillIn {

    width: 200px;
    height: 28px;
    border-radius: 4px;
    font-size: 16px;
    margin-bottom: 2px;

#searchHousing {

float: left;

}

Javascript: Javascript:

var inputSearch = document.getElementById('fillIn');

inputSearch.onclick = function() {
    var searchBox = document.createElement("div"); 
    searchBox.className = "searchBox";
    document.getElementById('searchHousing').appendChild(searchBox);
};

Thanks, Ewan 谢谢,伊万

I would suggest to not create new 'div' on click event. 我建议不要在点击事件上创建新的“ div”。 This will result in multiple "searchBox" divs when the input box is clicked multiple times. 多次单击输入框时,将导致多个“ searchBox” div。 Needless to say, it will require cleanup effort to remove duplicated divs. 不用说,将需要清理工作才能删除重复的div。

Rather, create the searchBox div in HTML and toggle it's visibility on javascript events. 而是以HTML创建searchBox div并在javascript事件上切换可见性。

Updated JSFiddle: http://jsfiddle.net/y7mEY/109/ 更新的JSFiddle: http : //jsfiddle.net/y7mEY/109/

[ [

var inputSearch = document.getElementById('fillIn');

inputSearch.onclick = function() {
    document.getElementById('searchBox').style.display = 'block';
};
inputSearch.onblur = function() {
    document.getElementById('searchBox').style.display = 'none';
};

] ]

If you want to use your current approach, simply use the "onblur" event to remove the element when the element loses focus. 如果要使用当前方法,只需在元素失去焦点时使用“ onblur”事件来删除该元素。

inputSearch.onblur = function() {
  document.getElementById('searchHousing').removeChild(document.getElementsByClassName("searchBox")[0]);
}

But as CVG mentions, creating/deleting elements live is a fairly bad idea. 但是正如CVG所提到的那样,实时创建/删除元素是一个非常糟糕的主意。

You'll be creating div elements all the time, which isn't any good. 您将一直创建div元素,这没有任何好处。 You could something along the lines of toggling the display attribute. 您可以按照切换display属性的方式进行操作。

Just to make sure, if you want the searchBox to stay open and only close if you're clicking outside the searchBox and fillin elements, then you can just follow the click events and only toggle the display if you're clicking outside both. 只是要确保,如果您希望searchBox保持打开状态,并且仅当您在searchBox fillin元素外部单击时才关闭,那么您可以仅跟随click事件,并且仅当在两个事件外部单击时才切换显示。 The other methods will just close it out as long as you're not typing in the input, and if you want to place tools or something within the div, then that would be useless. 只要您不输入输入内容,其他方法就会将其关闭,如果您想在div中放置工具或其他东西,那将毫无用处。

document.addEventListener('click', function(e) {
    if(e.target.id != "searchBox" && e.target.id != "fillIn")
        document.getElementById("searchBox").style.display="none";
});

Full demonstration: JSFiddle 完整的演示: JSFiddle

The problem with onBlur as the other answers suggest is that you will then hide the search options div every time you click anything in it (since the input field will lose focus). 其他答案表明onBlur的问题在于,您每次单击其中的任何内容时都会隐藏搜索选项div(因为输入字段将失去焦点)。 This is probably not what you're looking for. 这可能不是您想要的。 Instead, you can add a click handler for the document to hide the search options, and then add a click handler for the search housing to prevent event propagation (which keeps the document click handler from hiding the options). 相反,您可以为文档添加一个单击处理程序以隐藏搜索选项,然后为搜索外壳添加一个单击处理程序以防止事件传播(这使文档单击处理程序无法隐藏选项)。

  • If you click the input box, the handler shows the options 如果单击输入框,处理程序将显示选项
  • If you click the input box or the options, the event propagation stops 如果单击输入框或选项,事件传播将停止
  • If you click anywhere in the document besides the input box or options, the options are hidden 如果单击文档中除输入框或选项之外的任何位置,这些选项将被隐藏

Updated JSFiddle 更新了JSFiddle

var inputSearch = document.getElementById('fillIn');
var searchHousing = document.getElementById('searchHousing')

inputSearch.onclick = function(event) {
  document.getElementById('searchBox').style.display = 'block';
};
searchHousing.onclick = function(event) {
  event.stopPropagation(); // won't be passed to document.onclick
};
document.onclick = function() {
  document.getElementById('searchBox').style.display = 'none';
};

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

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