0

これは、誰かがDivの外部のどこかをクリックして閉じるときにウィンドウを閉じるためのjscriptです。私の質問は、誰かがアクションを実行してこれをクリックしたときにこのウィンドウを閉じることです。

<div id="box"
style="height: 3em; position:absolute; top: 20%; left: 15%; border: 3px double">
<p>Click anywhere outside this box to close it.
</div>
<script>
document.onclick = function (e) {
e = e || event
var target = e.target || e.srcElement
var box = document.getElementById("box")
do {
      if (box == target) {
           // Click occured inside the box, do nothing.
           return
      }
      target = target.parentNode
 } while (target)
 // Click was outside the box, hide it.
 box.style.display = "none"
}
</script>

DIV内でクリックが発生したときにDivを閉じる方法

4

3 に答える 3

0

ここで話している特定のことについては、私はそれをテストしませんでしたが、そのループを次のコードに変更することでそれを行うことができると思います。

do {
    if (box != target) {
       // Click occured outside the box, do nothing.
       return
    }
    target = target.parentNode
 } while (target)
于 2013-01-17T09:07:29.057 に答える
0

HTMLコード自体では、

<div id='box' style='height:10px; width:10px' onclick='CloseMe(this)'>...</div>

CloseMe関数を実装する

function CloseMe( obj )
{

    obj.style.display = 'none';
}
于 2013-01-17T09:07:42.057 に答える
0

JQueryを使用する場合は、関数event.stopPropagation();onを使用できます。click

$(function(){
  $('html').click(function() {
    $('#box').hide();
  });

  $('#box').click(function(event){
      event.stopPropagation();
  });
});

http://jsfiddle.net/nCqwy/1/

于 2013-01-17T09:13:59.833 に答える