1

I am using JQCloud for building a tagcloud. It's nice and easy and meets my user's visual criteria. I would like to have a click handler invoked when user clicks on a word:

var tag_list = new Array();
for ( var i = 0; i < stuff.length; ++i ) {
    var x = stuff[i];
    tag_list.push({
            text: x.NAME,
            weight: x.COUNT,
            //link: this.mkUrl(x),
            click: function() { alert("it worked for " + x.NAME); },
            html: {title: this.mkTooltip(x)}
    });
}
$("#"+containerdivname).append( $("<div></div>", {id:"wordcloud"}));
$("#"+containerdivname).children("#wordcloud").jQCloud( tag_list );

The word cloud renders fine, has proper tooltip, but does not show an alert box on click. What am I doing wrong here?

Thanks

4

3 回答 3

3

JQCloud 中的处理程序应该这样指定:

handlers : {click: function() { alert("it worked for" + x.NAME); }}

工作示例,http://jsfiddle.net/Q6348/7/

于 2013-06-11T07:25:31.873 回答
2

有点不同,也许更容易实现它的方法是,像这样构建你的数组:

var stuff = [];
stuff.push({"text":"n1","weight":23,"handlers" : {click: function(res) { alert("it worked for"+res.target.textContent);}}})

这将为您提供单击的节点的值。

或者如果你想将它传递给一个函数,你可以像这样使用它:

stuff.push({"text":"n1","weight":23,"handlers" : {click: function(res) { run(res) }}})

function run(res){
alert("it worked for"+res.target.textContent);
}

希望这个解决方案有帮助!

于 2017-03-27T09:51:29.820 回答
0

你总是可以为你的点击添加一个标准的 jquery 处理程序:

$(document).on('click', '#container-id .jqcloud-word', function() {
    var word_value = $(this).val();
    console.log(word_value);
});
于 2017-06-20T09:03:51.633 回答