非常奇怪的jquery问题

发布于 2024-12-10 18:11:37 字数 457 浏览 5 评论 0原文

我试图在页面加载时运行一个函数,但它不起作用。我已经将它添加到我能想到的每个地方,唯一有效的方法是:

$("html").mousemove(function(event) {
    $('#project_thumbs_container').masonry('reload');
});

我尝试过延迟,但我采用了上面的 hacky 方法:(

有没有人有任何建议为什么我的函数无法运行?

更新:

我正在使用 jquery 的 masonry。我的问题是当我加载使用 ajax 的 masonry 的页面时,它会在单个列中显示它们。 $('#project_thumbs_container').masonry('reload'); 可以正确重置它,但它只能使用上面的 mousemove 方法。

I am trying to run a function on page load but its not working. I've added it in every place I can think of and the ONLY thing that works is:

$("html").mousemove(function(event) {
    $('#project_thumbs_container').masonry('reload');
});

I've tried delays but I have resorted to the hacky above method :(

Does anyone have any suggestions as to why my function won't run?

UPDATE:

I am using masonry for jquery. My problem is when I load a page that uses masonry with ajax, it shows them in a single column. $('#project_thumbs_container').masonry('reload'); resets it properly, but it only works using the above mousemove method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心荒芜 2024-12-17 18:11:37

听起来您遇到了以下两个问题之一:

1) 格式错误的 HTML 导致错误,在使用文档 onReady 语法时不允许代码正确解析: $(function() { ... });

2) Masonry 可能会异步加载,这意味着“onReady”回调可能不是您想要使用的回调。您的 Ajax 调用看起来更像是这样:

$('body').load('index.html', function() {
  $('#project_thumbs_container').masonry();
});

It sounds like you have one of two problems:

1) Malformed HTML which is causing an error, which isn't allowing the code to parse correctly when using the document onReady syntax: $(function() { ... });

2) Masonry might be loading asynchronously, which means that the "onReady" callback might not be the one that you want to be using. Your Ajax call would look more like this:

$('body').load('index.html', function() {
  $('#project_thumbs_container').masonry();
});
梦在深巷 2024-12-17 18:11:37

除非有人有更好的答案,否则我只是在 ajax 调用完成后将代码放入我的 fadeIn(); 代码片段中:

this.fadeIn('slow', function() {
  $('#project_thumbs_container').masonry('reload');
});

似乎可以工作。

Unless someone has a better answer, I just put the code in my fadeIn(); snippet after ajax call is complete:

this.fadeIn('slow', function() {
  $('#project_thumbs_container').masonry('reload');
});

Seems to work.

不必了 2024-12-17 18:11:37

尝试这样的事情。

$(document).ready(function(){
    $('#project_thumbs_container').masonry('reload');
}); 

您可以将此代码放在页面上的任何位置,只要依赖项已加载,它就应该可以工作。

Try something like this.

$(document).ready(function(){
    $('#project_thumbs_container').masonry('reload');
}); 

You can put this code anywhere on the page and it should work, as long as the dependencies have already been loaded.

柏拉图鍀咏恒 2024-12-17 18:11:37

只需将您的函数放入其中,

$(document).ready(function() {
  // Handler for .ready() called.
  // your function 
});

当您的页面加载时,该函数就会执行

Just put your function in this

$(document).ready(function() {
  // Handler for .ready() called.
  // your function 
});

when your page load the function will execute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文