【问题标题】:jQuery: function gets triggered multiple timesjQuery:函数被多次触发
【发布时间】:2012-07-08 02:08:21
【问题描述】:

我有以下函数在某些 div (#block_profile) 上执行 qtip2 工具提示,问题是它被多次触发。因此,如果我单击第 4 个 #block_profile 它会调用此函数 4 次。我怎样才能让它只针对已点击的确切 div 执行?

// Create the tooltips only on document load
$(document).ready(function () {
    // Make sure to only match links to wikipedia with a rel tag
    $('div.block_profile[rel]').each(function () {


        // We make use of the .each() loop to gain access to each element via the "this" keyword...
        $(this).qtip(
            {
                content:{
                    // Set the text to an image HTML string with the correct src URL to the loading image you want to use
                    text:'<img src="/assets/ux/modal/loading.gif" alt="Loading..." />',
                    ajax:{
                        url:'/profiles/get_info/' + $(this).attr('rel') // Use the rel attribute of each element for the url to load
                    },
                    title:{
                        button:false
                    }
                },
                position:{
                    my:'top left',
                    target: 'mouse',
                    viewport:$(window), // Keep the tooltip on-screen at all times
                    adjust:{
                        x:10, y:10
                    }
                },
                hide:{
                    fixed:false // Helps to prevent the tooltip from hiding ocassionally when tracking!
                },
                style:{
                    classes:'container ui-tooltip ui-tooltip-tip'
                }
            })
    })

        // Make sure it doesn't follow the link when we click it
        .click(function (event) {
            event.preventDefault();
        });
});

html:

<div id ="block_profile" class ="block_profile rel="1">div 1</div>
<div id ="block_profile" class ="block_profile rel="2">div 2</div>
<div id ="block_profile" class ="block_profile rel="3">div 3</div>
<div id ="block_profile" class ="block_profile rel="4">div 4</div>
<div id ="block_profile" class ="block_profile rel="5">div 5</div>

【问题讨论】:

  • html 中的元素 ID 必须是唯一的。修复这些。
  • 您是否注意到在您的class 中缺少双引号?
  • 除了格式错误的 html,您的代码 works fine for me。 “点击”和“功能”多次触发是什么意思?除了event.preventDefault(),你没有任何点击功能。我相信,悬停的 qtip 工作正常?

标签: jquery load each


【解决方案1】:

您应该使用以下代码:

// Create the tooltips only on document load
$(document).ready(function () {
    // Make sure to only match links to wikipedia with a rel tag
    $('div.block_profile[rel]')
        .qtip({
            ...
        })
        .click(function (event) {
            event.preventDefault();
        });
});

each 调用不是必需的。以this demo 为例。

【讨论】:

  • 是的,但这不起作用。也许它的其他代码干扰了我的应用程序中有很多查询功能和插件。我认为我的代码很糟糕,但我必须验证这一点才能确定。谢谢
  • 如果这不起作用,则一定是其他问题。 each 电话真的不应该在那里。我建议您查看其他 cmets 以获取有关清理 html 的提示。
  • Thx 我计划清理它确实不是最整洁的 html,您的代码是正确的,应该可以工作,这确实是我在 html 中的失败
猜你喜欢
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
相关资源
最近更新 更多