JQuery llama a la lógica bind () dos veces, solo al recargar

I have the folllowing code:

$(document).ready(function () {
      $("#Titles .item a").each(function (index, domEle) {
        // 1)
      $(this).ttip($.extend(true, {}, ttip_opts, {
                content: {
                    text: $(this).attr('title'),
                    title: {
                        text: "SomeText"
                    }
                }
            }));
       // 2)
            $(this)
                .bind("click", function (event) {
                    alert(this);
                })
                .attr('title', '');
        });
});

When I click ‘reload’ on the page, and click the link, the alert statement shows up twice. On subsequent clicks, it appears once.

When I comment out statement #1, it only appears once every time.

What is happening here? How can I get the alert statement to display once every time? There is only one anchor element in the element I'm looping through.

Noticias

I still get two clicks on my method (now called from another script) even when I remove the bind to the click event. So the each function is definitely executing the logic in the click() method on reload only.

Still trying to figure out how I can determine if it's being called on reload, so I at least ignore the logic I don't want to run twice.

preguntado el 28 de agosto de 11 a las 00:08

did you forget to put your "$(document).ready(function () {" closing tag? -

Yes .. verified the closing tag is in the script. -

1 Respuestas

Creo que el ttip plugin is triggering the click event on the element which I think is not required. You can try this which stops the event propagation.

$(document).ready(function () {
      $("#Titles .item a").each(function (index, domEle) {
            // 2)
            $(this)
                .bind("click", function (event) {
                    alert(this);
                    return false;
            }).attr('title', '');

        // 1)
        $(this).ttip($.extend(true, {}, ttip_opts, {
                content: {
                    text: $(this).attr('title'),
                    title: {
                        text: "SomeText"
                    }
                }
        }));
    });
});

Respondido 28 ago 11, 04:08

That suggestion did not quite work. Changing the order around results in an invalid object reference for ttip. - duckus

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.