简体   繁体   English

单击时未调用jQuery函数

[英]Jquery function not being called on Clicking

I have a href url when I clicked that function I need to get some information but I oncliking the href Jquery function is not being called 单击该函数时,我有一个href网址,我需要获取一些信息,但没有调用oncliking href Jquery函数

Jquery code jQuery代码

$("#displayfood").click(function(){
    console.log("Function called")
});

Html code HTML代码

<div class="panel-heading">
    <h4 class="panel-title">
        <a href="#collapse1" id="displayfood" data-parent="#accordion2" 
           data-toggle="collapse" class="btn-block collapsed"> 
            <img class="img-rounded" src="images/db-icons/default-food.png"> Pizza
        </a>
    </h4>
</div>

try this: 尝试这个:

$(document).ready(function(){
   $("#displayfood").click(function(e){
      e.preventDefault();
      console.log("Function called");
    });
});

In the function you should use: "return" false or "event.preventDefault()" to stop the default event of click. 在该函数中,应使用:“ return” false或“ event.preventDefault()”停止默认的click事件。

like this : 像这样 :

$("#displayfood").click(function(){
       console.log("Function called")
       return false
 });

or: 要么:

$("#displayfood").click(function(e){
       console.log("Function called")
       e.preventDefault()
 });

Where did you put the jquery code? 您将jquery代码放在哪里? It needs to be after where you have defined the displayfood, otherwise that element will not be found when code executes. 它必须在定义displayfood的位置之后,否则在执行代码时将找不到该元素。 Alternatively, use the ready function. 或者,使用就绪功能。

You need to bind the event listener on document.ready. 您需要在document.ready上绑定事件监听器。

$(document).ready(function() {
    $("#displayfood").click(function(){
       console.log("Function called")
    });    
});

  $(document).ready(function() { $("#displayfood").click(function(){ console.log("Function called") }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="panel-heading"> <h4 class="panel-title"> <a href="#collapse1" id="displayfood" data-parent="#accordion2" data-toggle="collapse" class="btn-block collapsed"> <img class="img-rounded" src="images/db-icons/default-food.png"> Pizza </a> </h4> </div> 

Your code works fine, just ensure following things. 您的代码工作正常,只需确保遵循以下说明即可。

  1. Jquery is loaded jQuery已加载
  2. Close <img> tag with </img> </img>关闭<img>标记
  3. Wrap js code inside document.ready 将js代码包装在document.ready中

Reference: http://jsfiddle.net/fqLk9n02/ 参考: http : //jsfiddle.net/fqLk9n02/

If you use bootstrap because I saw the same structure but used wrong, check my example and on click works just fine ;) 如果您因为我看到的结构相同但使用了错误而使用了引导程序,请检查我的示例,然后单击就可以了;)

<div class="panel panel-info">
  <div class="panel-heading">
     <a href="#collapse1" id="displayfood" data-parent="#accordion2" 
       data-toggle="collapse" class="btn-block collapsed"> 
           <img class="img-rounded" src="images/db-icons/default-food.png"/> Pizza
    </a>
  </div>
  <div class="panel-body">
    Panel content
  </div>
</div>

$("#displayfood").click(function(){
    alert("Function called")
});

FIDDLE: 小提琴:

jquery .click is shorthand for .on("click") so use the .on("click"); jQuery .click是.on(“ click”)的简写,因此请使用.on(“ click”);

$("#displayfood").on("click",function(){
     console.log("Function called")
});

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

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