先有鸡还是先有蛋事件监听器原位:jQuery 监视 javascript 创建的节点?

发布于 2024-12-12 19:54:20 字数 757 浏览 1 评论 0原文

首先,我必须在 Javascript 文件中使用和创建这个 DOM 元素,但我坚持了下来。我无法在 jQuery 中完成这一切,这显然会容易得多。

简单来说:

JS 文件在页面加载后的某个时间点创建一个 dom 元素。 我想将画布图形附加到该 dom 元素。 该 canvas 元素是由 jQuery 插件触发的,我在我的网站中将其称为 jQuery 脚本,如下所示:

Spinners.create('#spinThis');
Spinners.create('#spinThis').play();});

我尝试使用“innerHTML”通过内联“script”标签将这段代码直接放入 Javascript 文件中,但它不是被看到(实际上为什么?)

这是一个真正的难题。我如何告诉我的 jQuery 文件通过另一个(Java)脚本不断寻找迟早会出现的元素,然后用它做一些事情?

我认为像 live() 这样的东西可以解决问题,但这没有意义,它需要一个事件,并且需要与尚不存在的 id 关联。 叹息

您可以在这里看到它:http://syndex.me

(从字面意义上来说, Javascript 文件是 tumblr 的无限滚动(唯一有效的),当它调用附加到内容包装器末尾的 div 中的新内容时,我会注入一个旋转预加载器,您可以将其视为末尾的粉红色条正在加载的帖子。)

First of all, I have to use and create this DOM element in a Javascript file, I'm stuck with it. I can't do this all in jQuery, which would obviously be a lot easier.

In basic terms:

The JS file creates a dom element at some point of time after the page has loaded.
I want to append a canvas graphic to that dom element.
That canvas element is triggered by a jQuery plugin, which I call in my sites jQuery script, like so:

Spinners.create('#spinThis');
Spinners.create('#spinThis').play();});

I have tried to put this bit of code directly in the Javascript file via inline "script" tags using "innerHTML" , but it's not being seen (why actually?)

It's a real conundrum. How do I tell my jQuery file to keep on the look out for an element which will come sooner or later,via another (Java) script, and then do something with it?

I would think something like live() would do the trick, but that doesn't make sense, it needs an event and it needs to be associated with the id which isn't even existent yet. Sigh

You can see it here: http://syndex.me

(In literal terms, the Javascript file is a infinite scroll for tumblr (the only one that works) and I'm injecting a spin preloader when it calls for new content in a div that is appended to the end of the content wrapper, you can see it as a pink bar at the end of the posts as it's loading.)

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

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

发布评论

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

评论(2

零度° 2024-12-19 19:54:22

这有点 hacky,但这基本上就是 livequery 过去所做的事情:

var spinThisTimer = setInterval(function(){

  if (document.getElementById('spinThis') {
    clearInterval(spinThisTimer);
    Spinners.create('#spinThis');
    Spinners.create('#spinThis').play();});
  });

},100);

它每秒在 DOM 中搜索 10 次,以查找 ID 为 spinThis 的元素。一旦找到,间隔就会被清除并运行代码。

It is sortof hacky, but this is basically what livequery used to do:

var spinThisTimer = setInterval(function(){

  if (document.getElementById('spinThis') {
    clearInterval(spinThisTimer);
    Spinners.create('#spinThis');
    Spinners.create('#spinThis').play();});
  });

},100);

It searches the DOM 10 times a second for an element with the ID of spinThis. Once it is found, the interval is cleared and the code is ran.

柠檬色的秋千 2024-12-19 19:54:22

我不确定我是否 100% 理解你的问题,但听起来你可以使用“live”来解决它。

请记住,您可以引用尚不存在的 id 或类并调用 live 方法,以便设置一个事件(甚至是自定义事件)来处理您需要的任何内容。

I'm not sure I understand 100% your problem but it sounds like you can use "live" to solve it.

Remember that you can reference an id or class that doesn't yet exist and call the live method, in order to set an event (even a custom event) that will handle whatever you need.

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