首页 > jquery的一个onmouseover

jquery的一个onmouseover

网上下载了一个热门城市的文字标签云,有些代码不是很懂, string_ 等于什么 ,function on_mouseover(e, ev)函数是什么意思, $("#my_words").jQCloud(word_list);中jqcloud是什么

    
    var string_ = "";
    for (var i = 0; i < data.length; i++) {
        var string_f = data[i][0];
        var string_n = data[i][1];
        string_ += "{text: '" + string_f + "', weight: '" + string_n + "',html: {'class': 'span_list',onmouseover:'on_mouseover(this,event)',onmouseout:'on_mouseout()'}},";
    }

    function on_mouseover(e, ev) {
        var txt = $(e).html();
        $.each(data, function(i, item) {
            if (txt == item[0]) {
                var html = item[0] + "<br />频率:" + item[1];
                $("#my_words").after("<div class='append_div' style='left:" + ev.clientX + "px; top:" + ev.clientY + "px; '>" + html + "</div>");
                return;
            }
        });
    }
    $(function() {
        $("#my_words").jQCloud(word_list);
    });
    var string_list = string_;
    var word_list = eval("[" + string_list + "]");

    function on_mouseout() {
        $(".append_div").remove();
    }

注释里有简单的解释

// 这一段应该是在拼(凑)数据
var string_ = "";
for (var i = 0; i < data.length; i++) {
    var string_f = data[i][0];
    var string_n = data[i][1];
    // 每次循环会往 string_ 里加一个对象的类(似) JSON 数据(之所以说是类 JSON,
    // 因为它不符合 JSON 的定义,但是这个字符串的内容是一个 JavaScript 对象的描述
    string_ += "{text: '" + string_f + "', weight: '" + string_n + "',html: {'class': 'span_list',onmouseover:'on_mouseover(this,event)',onmouseout:'on_mouseout()'}},";
}

// 定义一个函数,看样子应该是绑定给某个对象的 mouseover 事件
function on_mouseover(e, ev) {
    var txt = $(e).html();
    $.each(data, function(i, item) {
        if (txt == item[0]) {
            var html = item[0] + "<br />频率:" + item[1];
            $("#my_words").after("<div class='append_div' style='left:" + ev.clientX + "px; top:" + ev.clientY + "px; '>" + html + "</div>");
            return;
        }
    });
}

// jQCloud 是一个插件,看这里:http://www.oschina.net/p/jqcloud
$(function() {
    $("#my_words").jQCloud(word_list);
});

// 这里在使用上面拼出来的 string_,在它的前后加了 [],表示这是一个数组数据描述
var string_list = string_;
var word_list = eval("[" + string_list + "]");

// 定义一个函数,也应该是在某个地方绑定给某个对象的 mouseout 事件的
function on_mouseout() {
    $(".append_div").remove();
}

// mouseover 和 mouseout 事件通常成对出现,不过至少在都出现的情况下,放在一起比较好看

不过拼数据那一块简直太弱了,jQCloud 要的本来就是个 JavaScript 数组,直接给数据就行,为毛要拼成字符串再来 eval

是我就会这样写,好更好写也更好懂。

var word_list = [];
for (var i = 0; i < data.length; i++) {
    word_list.push({
        text: string_f,
        weight: string_n,
        html: {
            class: "span_list",
            onmouseover: "on_mouseover(this, event)",
            onmouseout: "on_mouseout()"
        }
    });
}

$(function() {
    $("#my_words").jQCloud(word_list);
});
【热门文章】
【热门文章】