Can';t将hover与jQuery标记云一起使用

Can';t将hover与jQuery标记云一起使用,jquery,tag-cloud,Jquery,Tag Cloud,当人们将鼠标悬停在标记云中的任何项目上时,我需要添加一个onHover事件,下面是代码。。我想某处有冲突。现在它根本不会触发 <html> <head> <title>jQCloud Example</title> <link rel="stylesheet" type="text/css" href="../jqcloud/jqcloud.css" /> <script t

当人们将鼠标悬停在标记云中的任何项目上时,我需要添加一个onHover事件,下面是代码。。我想某处有冲突。现在它根本不会触发

<html>
    <head>
        <title>jQCloud Example</title>
        <link rel="stylesheet" type="text/css" href="../jqcloud/jqcloud.css" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
        <script type="text/javascript" src="../jqcloud/jqcloud-1.0.3.js"></script>
        <script type="text/javascript">
            var word_list = [
                {text: "Lorem", weight: 13, link: "https://github.com/DukeLeNoir/jQCloud"},
                {text: "Ipsum", weight: 10.5, html: {title: "My Title", "class": "custom-class"}, link: {href: "http://jquery.com/", target: "_blank"}},
                {text: "Dolor", weight: 9.4},
                {text: "Sit", weight: 8},
                {text: "Amet", weight: 6.2},
                {text: "Consectetur", weight: 5},
                {text: "Adipiscing", weight: 5},
                {text: "Elit", weight: 5},
                {text: "Nam et", weight: 5},
                {text: "Leo", weight: 4},
                {text: "Sapien", weight: 4},
                {text: "Pellentesque", weight: 3},
                {text: "habitant", weight: 3},
                {text: "morbi", weight: 3},
                {text: "tristisque", weight: 3},
                {text: "senectus", weight: 3},
                {text: "et netus", weight: 3},
                {text: "et malesuada", weight: 3},
                {text: "fames", weight: 2},
                {text: "ac turpis", weight: 2},
                {text: "egestas", weight: 2},
                {text: "Aenean", weight: 2},
                {text: "vestibulum", weight: 2},
                {text: "elit", weight: 2},
                {text: "sit amet", weight: 2},
                {text: "metus", weight: 2},
                {text: "adipiscing", weight: 2},
                {text: "ut ultrices", weight: 2},
                {text: "justo", weight: 1},
                {text: "dictum", weight: 1},
                {text: "Ut et leo", weight: 1},
                {text: "metus", weight: 1},
                {text: "at molestie", weight: 1},
                {text: "purus", weight: 1},
                {text: "Curabitur", weight: 1},
                {text: "diam", weight: 1},
                {text: "dui", weight: 1},
                {text: "ullamcorper", weight: 1},
                {text: "id vuluptate ut", weight: 1},
                {text: "mattis", weight: 1},
                {text: "et nulla", weight: 1},
                {text: "Sed", weight: 1}
            ];
            $(function() {
                $("#my_favorite_latin_words").jQCloud(word_list);
                $('span').hover(function() {
                    alert('a');
                });
            });
        </script>
    </head>
    <body>
        <h1>jQCloud Example</h1>
        <div id="my_favorite_latin_words" style="width: 550px; height: 350px; border: 1px solid #ccc;"></div>
    </body>
</html>

jQCloud示例
变量字列表=[
{文本:“Lorem”,重量:13,链接:https://github.com/DukeLeNoir/jQCloud"},
{文本:“Ipsum”,权重:10.5,html:{title:“我的标题”,“类”:“自定义类”},链接:{href:http://jquery.com/,目标:“\u blank”}},
{文本:“多洛”,重量:9.4},
{正文:“坐”,体重:8},
{文本:“Amet”,重量:6.2},
{文本:“Concertetur”,重量:5},
{文本:“adipising”,权重:5},
{文本:“精英”,权重:5},
{文本:“Nam et”,权重:5},
{文本:“狮子座”,权重:4},
{文本:“Sapien”,重量:4},
{文本:“佩伦茨克”,重量:3},
{文本:“居住者”,体重:3},
{正文:“morbi”,重量:3},
{文本:“三色”,重量:3},
{文本:“sentecus”,权重:3},
{文本:“et netus”,权重:3},
{文本:“et malesuada”,重量:3},
{文本:“名人”,权重:2},
{文本:“ac turpis”,重量:2},
{文本:“egestas”,重量:2},
{文本:“埃尼安”,重量:2},
{正文:“前庭”,重量:2},
{文本:“精英”,权重:2},
{文本:“坐在阿梅特”,重量:2},
{文本:“metus”,重量:2},
{文本:“adipising”,权重:2},
{文本:“ut ultrices”,权重:2},
{文本:“justo”,权重:1},
{正文:“格言”,权重:1},
{文本:“Ut et leo”,权重:1},
{文本:“metus”,权重:1},
{文本:“在摩尔斯蒂”,重量:1},
{文本:“purus”,重量:1},
{正文:“库拉比图尔”,重量:1},
{文本:“直径”,重量:1},
{文本:“酒后驾车”,体重:1},
{文本:“ullamcorper”,重量:1},
{文本:“id vuluptate ut”,权重:1},
{文本:“mattis”,权重:1},
{文本:“et nulla”,权重:1},
{文本:“Sed”,权重:1}
];
$(函数(){
$(“我最喜欢的拉丁单词”).jQCloud(单词列表);
$('span')。悬停(函数(){
警报(“a”);
});
});
jQCloud示例
原因

hover
处理程序正在绑定到页面加载中尚不存在的
span
元素。因此,处理程序不会绑定到动态添加的
span
元素

解决方案

您不应该直接绑定处理程序函数,而应该将它在DOM的更高层委托给页面加载中存在的父元素

例如:

$("#my_favorite_latin_words").on('mouseenter', 'span', function() {
    alert('a');
});

在jqcloud制作完wordcloud后,使用jqcloud的
afterCloudRender
运行jquery

$('#example').jQCloud(words,{
    shape : 'rectangle',
    afterCloudRender : function(){
        $('span','#example').each(function(){
            alert($(this).text());
        });
    }
});