对于通过ajax注入的html,重新运行 document.ready() jqueries,可以吗?
here is my case:
<html>
<body>
<head>
...
<script>
$(function(){
$('.do-tip').qtip({
content: 'This is a tip of active hovered element',
show: 'mouseover',
hide: 'mouseout',
})
});
</script>
</head>
<body>
<a href="http://www.google.com" class="do-tip">google</a>
<input type="button" value="load div by ajax" />
<div> <!-- this div loaded by ajax -->
<div>
<a href="http://www.yahoo.com" class="do-tip">yahoo</a> <!-- HOW TO HERE, run the existing script only for this part, JQUERY UNCLE must have a solution-->
</body>
</html>
any ideas?
here is my case:
<html>
<body>
<head>
...
<script>
$(function(){
$('.do-tip').qtip({
content: 'This is a tip of active hovered element',
show: 'mouseover',
hide: 'mouseout',
})
});
</script>
</head>
<body>
<a href="http://www.google.com" class="do-tip">google</a>
<input type="button" value="load div by ajax" />
<div> <!-- this div loaded by ajax -->
<div>
<a href="http://www.yahoo.com" class="do-tip">yahoo</a> <!-- HOW TO HERE, run the existing script only for this part, JQUERY UNCLE must have a solution-->
</body>
</html>
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然尚不完全清楚您想要做什么,但我会尝试我的 MagicGuess 工具并为您提供答案。
因此,您需要
a) 在文档加载上执行一些功能;此功能对页面上的所有
.do-tip
元素执行某些操作b) 在通过 AJAX 加载某些内容后执行相同的功能,但现在需要与另一组元素一起操作。
这是真的吗?如果是这样,我会这样做:
在通过 AJAX 加载 HTML 后,使用另一个选择器调用
doEverythingINeed
。如果您使用$.ajax()
函数,那么您应该这样做希望有帮助!
While it is not perfectly clear what you are trying to do, I will try my MagicGuess tool and offer you an answer.
So, you need to
a) perform some functionality on document load; this functionality does something with all
.do-tip
elements on the pageb) perform the same functionality after you load something via AJAX, but now this needs to operate with another set of elements.
Is that true? If so, here is what I would do:
and call
doEverythingINeed
with another selector after you load your HTML via AJAX. If you use$.ajax()
function, then you should just doHope that helps!
为了防止它对其他人有帮助,我有一个非常相似的用例,并使用 jQuery 的 .live() 事件绑定器解决了它 - 比我试图通过重新处理所有 $('document').ready 来设计的解决方案简单得多() 事件。
用例:一个页面上有很多链接(HTML a 标签)。在 $('document').ready() 上,我将相关链接的 .click() 事件绑定到一个调用某些 .ajax() 的函数,该函数检索更多相同类型的链接并将其 HTML 注入到页面中。问题是,新链接没有绑定。单击新链接不会像应有的那样触发 .ajax() 代码。
我花了很长时间尝试在注入 HTML 后重新触发 read 事件,然后意识到使用 .live('click) 而不是 .click() 绑定事件将给出完全相同的结果,且额外代码为零。
顺便说一句,其他人在其他地方为原始问题提供了非常好的解决方案 - 请参阅问题 2238030
Just in case it helps someone else, I had a very similar use case and solved it with jQuery's .live() event binder - much simpler than the solution I was trying to engineer by re-processing all $('document').ready() events.
The use case: A page with a lot of links (HTML a tags) on it. On $('document').ready() I bound the .click() events of the relevant links to a function that called some .ajax(), which retrieved more links of the same type and injected their HTML into the page. Problem was, the new links weren't bound. Clicking on the new links didn't trigger the .ajax() code like it should.
I spent ages trying to re-trigger the ready event after the HTML was injected and then realised that binding the events with .live('click) instead of .click() would give exactly the same result with zero extra code.
By the way, someone else has given a very good solution to the original question elsewhere - see question 2238030
您可以在就绪函数中构建模块执行控制器,并在注入 HTML 后重新运行这些函数。像这样的东西:
https://gist.github.com/miguel-perez/476046a42d229251fec3
You can build in a module execution controller into the ready function and re-run the functions after you've injected the HTML. Something like this:
https://gist.github.com/miguel-perez/476046a42d229251fec3