简体   繁体   English

jQuery函数不会在首次单击时触发

[英]jQuery function not triggering on first click

I am having a small problem where my click function is triggering on the first click. 我有一个小问题,我的点击功能是在第一次点击时触发的。 The function does work on the second click though. 但是,该功能确实可以在第二次单击时使用。 Below is an example of my code. 以下是我的代码示例。

function returnReviews() {
$(".bv-read-review ").click(function() {
$('html, body').animate({
    scrollTop: $(".bv-action-bar-header").offset().top
}, 700);
});
}; 

The second part of my code where the function is called is here, below is an example of the relevant part. 我的代码的第二部分(在此处调用该函数)在此处,下面是相关部分的示例。

$(document).ready(function() {
var bvrrDiv = $('#bvReview');
var productId = bvrrDiv.attr("data-productid");
    bvrrDiv.append("<a class='bv-read-review' data-productid='" + productId      + "' href='javascript:returnReviews();'\">Read reviews</a>");

What i am trying to achieve is on the first click of a link for the screen to scroll down to a particular div. 我想要实现的是在链接的第一次单击上,以便屏幕向下滚动到特定的div。

The first function sits at the top of the JS file whilst the second sits at the bottom. 第一个函数位于JS文件的顶部,而第二个函数位于底部。

I'm relatively new at jQuery so any help would be greatly appreciated. 我在jQuery方面相对较新,因此任何帮助将不胜感激。

Kind Regards, 亲切的问候,

You're calling the function when the element is clicked: 单击元素时,您正在调用该函数:

href='javascript:returnReviews();'

But what does that function do ?: 但是该功能什么作用?:

function returnReviews() {
    $(".bv-read-review ").click(function() {
        //...
    });
}

It just adds a click handler. 它只是添加了一个点击处理程序。 Nothing else. 没有其他的。 The next time you click the element, that click handler will be invoked. 下次单击该元素时,将调用该单击处理程序。 (And another one will be added, over and over, until you have many click handlers all trying to do the same thing at the same time.) (然后将一遍又一遍地添加另一个,直到您有许多单击处理程序都试图同时执行相同操作为止。)

Instead of wrapping it in a function and calling that function in-line, just add the click handler to the page: 无需将其包装在函数中并内联调用该函数,只需将click处理程序添加到页面中:

$(document).on('click', '.bv-read-review', function() {
    $('html, body').animate({
        scrollTop: $(".bv-action-bar-header").offset().top
    }, 700);
});

And then you don't have to add it in-line in the markup: 然后,您不必在标记中内联添加它:

bvrrDiv.append("<a class='bv-read-review' data-productid='" + productId + "' href='#'\">Read reviews</a>");

Note that a non-link href is somewhat bad practice. 注意,非链接href有点不好。 Since this isn't actually an "anchor" per se, then a isn't really the correct tag to use. 由于这并不是真正的“锚”本身,那么a是不是真的要使用正确的标签。 A button styled to look like an anchor, perhaps? 看起来像锚的样式的button ,也许吗? A span which styles the cursor when hovering? 悬停时为光标设置样式的span There are lots of options. 有很多选择。 What you have will technically work , but may cause confusion for accessibility (screen readers, other non-visual browsers, etc.) if that's a concern. 从技术上讲,您所拥有的会起作用 ,但是如果您担心的话,可能会导致可访问性(屏幕阅读器,其他非可视浏览器等)混乱。

The element should be added to the DOM with the click handler bound to it. 应将元素与绑定的单击处理程序添加到DOM。 The way you have it written currently, you have a function called on click, which then adds a new click handler. 当前编写方式,您有一个在click上调用的函数,该函数随后添加了一个新的click处理程序。 With JQuery you can completely avoid putting JavaScript click handlers directly on your elements like that. 使用JQuery,您可以完全避免将JavaScript点击处理程序直接放在这样的元素上。 Anytime you call your returnReviews() function it will bind another instance of your click handler to the click event. 每当您调用returnReviews()函数时,它将把您的点击处理程序的另一个实例绑定到click事件。 Here's a simple example: $('<a>link</a>').on('click',function(event){ /* do stuff */ }).appendTo('#somediv'); 这是一个简单的示例: $('<a>link</a>').on('click',function(event){ /* do stuff */ }).appendTo('#somediv');

As david said.. plus a cleaner approach would be better: jsfiddle 正如大卫所说..再加上一种更清洁的方法会更好: jsfiddle

function returnReviews() {
  $('html, body').animate({
        scrollTop: $(".bv-action-bar-header").offset().top
    }, 700);
};
$(document).ready(function() {
  var bvrrDiv = $('#bvReview');
  var productId = bvrrDiv.attr("data-productid");
  jQuery('<a/>', {
    "href": "#",
    "data-productid": productId,
    "class": "bv-read-review",
    "click": returnReviews,
    "text": "Read reviews"
  }).appendTo(bvrrDiv);
});

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

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