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
and gc_ba_analysis
, so $(gc_abstract_heading).click()
in the console works, just not in the code.
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. 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:
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选择器缺少“#”。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.