单击div容器外部时的Focusout

时间:2015-11-16 14:44:30

标签: javascript jquery css html5

我有一小段代码,我想知道当我点击div class="gutte"之外的时候该怎么办,这个div应该是焦点。当我点击div class="gutte"(或<body>)之外并且我无法使用input type[text]时,我会将代码向下移动并且这不是焦点。当我点击btn1 div class="gutte"焦点但我点击另一个部分(输入类型文本)时,它会聚焦。我怎么能这样做?我正在使用jQuery框架。

var tt = $.noConflict();
tt(document).ready(function($) {
  $(".qta").focus(function() {

    $(".gutt").css({
      'display': 'inline-block'
    });
    $(".gutte").addClass('grandisciti');
  });

  $(".gutte").focusout(function() {

    $(".gutt").css({
      'display': 'none'
    });
    $(".gutte").removeClass('grandisciti');
  });
});
body {
	  width: 100%;
	  height: 100%;
	  background-color: #c1c2c3;
	}
	.gutte {
	  box-sizing: border-box;
	  position: relative;
	  display: block;
	  width: 550px;
	  height: 50px;
	  background-color: #fff;
	  color: #000;
	  margin: 0px;
	  padding: 0px;
	  margin-left: 32%;
	  margin-top: 30px;
	  padding-top: 2.5px;
	  padding-left: 1px;
	  padding-bottom: 2px;
	  border-radius: 10px;
	  border-radius: 10px;
	  text-align: center;
	}
	.ati {
	  box-sizing: border-box;
	  position: static;
	  display: inline-block;
	  width: 105px;
	  height: 45px;
	  margin: 0px;
	  padding: 0px;
	  padding: 0px 10px;
	  border: none;
	  background-color: #ccc;
	  /*border:2px solid #ccc;*/
	  border-radius: 10px;
	  outline: none;
	  cursor: pointer;
	  transition: 0.8s all;
	  font-size: 120%;
	  color: #000;
	}
	.grandisciti {
	  width: 600;
	  height: 300px;
	  -webkit-box-shadow: 2px 2px 5px 6px rgba(0, 0, 0, 0.5);
	  -moz-box-shadow: 2px 2px 5px 6px rgba(0, 0, 0, 0.5);
	  box-shadow: 2px 2px 5px 6px rgba(0, 0, 0, 0.5);
	}
	.gutt {
	  display: none;
	  width: 530px;
      margin:10px;
	  background-color: #000;
	}
	.tref {
	  display: inline-block;
	  width: 80%;
	  height: 100px;
	  resize: none;
	  outline: none;
	  padding: 10px;
	  margin: 15px;
	  border: 5px solid #ccc;
	}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

body
<div tabindex="1" class="gutte">
  <div  class="irj fjr_p05gure rddre ">
    <button class="ati qta ">btn1</button>
    <button class="ati ">btn2</button>
    <button class="ati ">bt3</button>

  </div>
  <span class="gutt">
				<input type="text" class="tref"/>
  </span>

</div>

1 个答案:

答案 0 :(得分:1)

当您在一个元素中聚焦时,任何聚焦的元素都会聚焦,然后聚焦目标元素。请参阅此示例以便更好地理解。

&#13;
&#13;
$("input").focus(function(){
    console.log(this.name + " focused");
}).blur(function(){
    console.log(this.name + " blured");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="A" /> 
<input name="B" /> 
<input name="C" />
&#13;
&#13;
&#13;

您需要将focus事件添加到任何button,如下所示:

<强>答案: Jsfiddle