简体   繁体   English

Javascript jQuery单击功能不起作用

[英]Javascript jQuery click function doesn't work

I have a very confusing problem. 我有一个非常困惑的问题。 I want to do some action within an HTML site, when a link is clicked. 单击链接后,我想在HTML站点内执行一些操作。 I implemented the following code with a click event, which should handle this behaviour: 我使用click事件实现了以下代码,该事件应能够处理此行为:

$(document).ready(function() {
  console.log("Ready!");
  $( "a" ).click(function() {
    console.log("Alert!");
    alert("Test");
  });
});

I implmented it in the head and at the end of the body but nothing happens. 我将其安装在头部和身体末端,但没有任何反应。 I don't get the console.log or alert. 我没有console.log或警报。 When I change the click to mouseover everything ist fine. 当我将单击更改为鼠标悬停时,一切正常。 Do you have any suggestions? 你有什么建议吗?

Regards, Jens 问候,詹斯

try using 尝试使用

$(document).ready(function() {
  console.log("Ready!");
  $( "a" ).on("click",function() {
    console.log("Alert!");
    alert("Test");
  });
});

because some versions of Jquery don't support the click function alone 因为某些版本的Jquery不单独支持click功能

Is the a present when the page is loaded? a当页面加载礼物? If not, you need to modify your event binding a little. 如果没有,则需要稍微修改事件绑定。 Also, you can try to prevent the default behavior. 此外,您可以尝试防止默认行为。

$(document).ready(function() {
  console.log("Ready!");
  $(document).click("a", function(e) {
    e.preventDefault();
    console.log("Alert!");
    alert("Test");
  });
});

The first thing that you need to do is check you jquery library to make sure that you have included it or not? 您需要做的第一件事是检查您的jquery库,以确保您已将其包括在内?

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>

If it still doesn't work you should check your anchor attribute and change it like bellow 如果仍然无法正常工作,则应检查锚点属性并像下面这样更改

<a href="#">alert</a> or <a>alert</a>

So this code will work 所以这段代码会起作用

 $(document).ready(function() {
  console.log("Ready!");
  $( "a" ).click(function() {
    console.log("Alert!");
    alert("Test");
  });
});

Make sure to include Jquery in your head: 确保在您的头脑中包括Jquery:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">    </script>
  <script>
  $(document).ready(function() {
  console.log("Ready!");
  $('a[href$="google"]').on("click",function() {
    console.log("Alert!");
    alert("Test");
  });
});
</script>
</head>
<body>
  <a href="google">google</a>
</body>
</html>

Otherwise you can get DOM elements by using: getElementsById and add id in your link 否则,您可以使用以下方法获取DOM元素:getElementsById并在链接中添加ID

please see this : jsfiddle 请看这个: jsfiddle

$(document).ready(function() {
        console.log("Bereit!");
        $( "a" ).click(function() {
            console.log("Alert!");
            alert("BBB wurden mit dem CCC synchronisiert");
        });
    });

its works. 其作品。

Are you sure you've added the correct version jQuery? 您确定添加了正确的jQuery版本吗?

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

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