简体   繁体   English

jQuery插件Hovercard在动态生成的内容中不起作用

[英]Jquery plugin hovercard not work in dynamically generated content

Jquery plugin Hovercard works well in simple HTML code but does not work when i used it in javascript code (dynamically creating html code through javascript). jQuery插件Hovercard在简单的HTML代码中效果很好,但是在JavaScript代码(通过JavaScript动态创建html代码)中使用时却无法正常工作。 Maybe this is also a general problem, how can jquery plugin work in js code generated content? 也许这也是一个普遍的问题,jQuery插件如何在js代码生成的内容中工作?

I haven't seen any of your code so I can't be sure that this will help. 我还没有看到您的任何代码,所以不确定是否会有所帮助。 But the issue with adding HTML dynamically with JS is that the resulting DOM elements would not have the necessary event handlers attached to them (in the case of Hovercard, mouseover , mouseout and probably others). 但是,使用JS动态添加HTML的问题在于,生成的DOM元素将没有附加必要的事件处理程序(在Hovercard, mouseovermouseout和其他情况下)。 The solution would be to call $(...).hovercard({...}) on the newly added DOM elements after you add them. 解决方案是在添加新的DOM元素调用$(...).hovercard({...}) eg 例如

$("body").append("<a class='hovercard'>New element.</a>");
$("body a.hovercard").hovercard({...});

After this block, the a tag would have the Hovercard effect. 此块后, a标签将有名片的作用。 If you call hovercard() on some elements in $(document).ready() , then only those elements which are present when the DOMContentLoaded event fires will have the effect (not any elements you add later). 如果对$(document).ready()某些元素调用hovercard() ,则只有DOMContentLoaded事件触发时出现的那些元素才会生效(以后不会添加任何元素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM