对于Span,Jquery Click事件未触发

时间:2018-03-19 07:04:11

标签: jquery html

当我将ID移至<i>标记时,点击事件不会触发范围或<i>

&#13;
&#13;
$("body").on("click", "#btnClear", function() {
  alert("Clicked");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <span title="Tüm Liste" style="cursor: pointer;" class="input-group-addon" id="btnClear"><i class="glyphicon glyphicon-home"></i></span>
  <input type="text" class="form-control" id="txtSearch" placeholder="GTIP KOD veya Ürün Adı" aria-describedby="basic-addon2">
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

我认为您的代码正常运行。除了您忘记加载jquery之外没有问题

$("body").on("click", "#btnClear", function() {
  alert("Clicked");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <span title="Tüm Liste" style="cursor: pointer;" class="input-group-addon" id="btnClear">ICON HERE.. CLICK</span>
  <input type="text" class="form-control" id="txtSearch" placeholder="GTIP KOD veya Ürün Adı" aria-describedby="basic-addon2">
</div>

我没有显示icon,所以我点了一个文字点击那里。

希望它有所帮助。

答案 1 :(得分:0)

试试这个。

&#13;
&#13;
$("body").delegate("#btnClear", "click", function () {
    alert("Clicked");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<div class="input-group">
   <span title="Tüm Liste" style="cursor: pointer;" class="input-group-addon" id="btnClear">
   <i class="glyphicon glyphicon-home"></i>
   </span>
   <input type="text" class="form-control" id="txtSearch" placeholder="GTIP KOD veya Ürün Adı" aria-describedby="basic-addon2">
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您的范围未显示,这就是问题所在。

你JS工作正常。我给了你的跨度。

$("body").on("click", "#btnClear", function() {
  alert("Clicked");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">

<div class="input-group">

  <span title="Tüm Liste" style="cursor: pointer; min-width:40px;" class="input-group-addo" id="btnClear"><i class="glyphicon glyphicon-home"></i></span>
  <input type="text" class="form-control" id="txtSearch" placeholder="GTIP KOD veya Ürün Adı" aria-describedby="basic-addon2">
</div>