简体   繁体   English

页面加载后的.click()

[英].click() after page load

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. 我第一次尝试使用URL持久性,并且在页面加载后,我具有要从URL单击的元素。 When I load the page, the .click() does not register, but if i type it in the console, it performs correctly. 当我加载页面时,.click()不会注册,但是如果我在控制台中键入它,它将正确执行。 I have tried placing the code(specifically the (function persistence(formvals){...})(); ) in the (document).ready(function(){...}) section, but that did not work. 我试过将代码(特别是(function persistence(formvals){...})(); )放在(document).ready(function(){...})部分中,但这没有用。 How do I get the .click() to register AFTER the page load, and on each element that should be clicked? 如何获得.click()以在页面加载后以及应单击的每个元素上进行注册?

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

the two ids of these sections are : gc_abstract_heading and gc_ba_analysis , so $(gc_abstract_heading).click() in the console works, just not in the code. 这些部分的两个ID是: gc_abstract_headinggc_ba_analysis ,因此控制台中的$(gc_abstract_heading).click()有效,只是在代码中无效。

code: 码:

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. 实时页面上的持久性部分有所不同。

Try this: 尝试这个:

$(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. 如果网址中的名称指向ID,则点击处理程序需要使用#作为前缀。 If it's a class you change # with . 如果是班级,请使用更改# . .

在这里黑暗中快速刺杀,但我还是可以找到您是否在持久性中单击或在dom之后使用jquery中的实时单击更安全。

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

you must call persistence() on document load (or ready) and also the formvals declaration: 您必须在document load (或就绪)以及formvals声明时调用persistence()

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);
});

如果您要定位ID,则jQuery选择器缺少“#”。

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

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