gpt4 book ai didi

jQuery 有没有办法在继续之前等待对象加载?

转载 作者:行者123 更新时间:2023-12-01 00:18:49 25 4
gpt4 key购买 nike

我有一个与 cookie 有关的问题。我有一组按钮,显示与按下的按钮相关的特定表格。我将其设置为分配 cookie 值,并尝试让它记住上次查看的内容,这样用户就不必在每次关闭页面时重新选择所有内容。

if($.cookie('lastviewed') == "value") {
$('#Button1').trigger('click');
$('#Button2').trigger('click');
}

按钮 1 加载到按钮 2 中,因此上面的方法不起作用,因为它试图触发尚不存在的东西。

有没有办法让它等待加载后再继续?

编辑:我已经在使用 document.ready。单击按钮 1 将按钮 2 加载到页面上,我需要它等待按钮 2 中的加载完成,然后再尝试单击它。

最佳答案

您需要使用document.ready确保 html 元素在被 javascript/jQuery 访问之前可用。

$(document).ready(function(){
if($.cookie('lastviewed') == "#value") {
$('#Button1').trigger('click');
$('#Button2').trigger('click');
}
});

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. Reference

关于jQuery 有没有办法在继续之前等待对象加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15828395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com