.click() después de cargar la página

I am trying url persistence for the first time, and after a page load, I have the elements that I want to click from the URL. When I load the page, the .click() does not register, but if i type it in the console, it performs correctly. I have tried placing the code(specifically the (function persistence(formvals){...})();) in the (document).ready(function(){...}) section, but that did not work. How do I get the .click() to register AFTER the page load, and on each element that should be clicked?

url: http://specialorange.org/resume/index.html?gc_abstract_heading&gc_ba_analysis

the two ids of these sections are : gc_abstract_heading y gc_ba_analysis, asi que $(gc_abstract_heading).click() in the console works, just not in the code.

código:

var formvals = {};
var keyval = location.search.replace('?', '').split('&');
$.each(keyval, function () {
    var splitval = this.split('=');
    formvals[splitval[0]] = splitval[1];
});
console.log(keyval);
console.log(formvals);

(function persistence(formvals) {
    for ( i=0 ; i < keyval.length ; i++ ) {
        console.log(keyval[i]);         
        $(keyval[i]).click();
    };
})();

Please note that this code is not on the live page, it is on my local site for testing. The persistence section on the live page is different.

preguntado el 31 de julio de 12 a las 10:07

4 Respuestas

Prueba esto:

$(document).ready(function () {
    var formvals = {};
    var keyval = location.search.replace('?', '').split('&');
    for (var i = 0; i < keyval.length; i++) {
        console.log('Trigger click on: '+keyval[i]);
        $('#'+keyval[i]).click();
    };
});

First I've added document-ready, so the function does not fire until it's ready. Second; the click-handler need # as a prefix if the name in the url points towards an id. If it's a class you change # con ..

Respondido 31 Jul 12, 11:07

Quick stab in the dark here but I allways find if your doing stuff with click in persistance or after the dom its safer to use the live click in jquery.

$(keyval[i]).live("click",function(){});

Respondido 31 Jul 12, 10:07

as of jQuery 1.7, the live el método está en desuso - Pedro l.

true if your using 1.7 its better to use .on() or .delegate() - Dominic Green

I can use .on() even though the user isn't 'click'ing the elements? - chris frisina

The jQuery Selector is missing the '#' if you are targeting IDs.

Respondido 31 Jul 12, 11:07

debes llamar persistence() on document load (or ready) and also the formvals declaración:

function persistence(formvals) {
    for ( i=0 ; i < keyval.length ; i++ ) {
        console.log(keyval[i]);         
        $(keyval[i]).click();
    };
}

$(function() {
    var formvals = {};
    var keyval = location.search.replace('?', '').split('&');
    $.each(keyval, function () {
        var splitval = this.split('=');
        formvals[splitval[0]] = splitval[1];
    });
    console.log(keyval);
    console.log(formvals);

    persistence(formvals);
});

Respondido 31 Jul 12, 11:07

document load mean the function $(document).ready(function () {..}); ? - chris frisina

onload events occurs a little later than onready (when all content is loaded). $(function(){ ... }) is a shorthand to document ready. - Pedro l.

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