how to provide a click handler in JQCloud

核能气质少年 提交于 2019-12-08 06:09:43

问题


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


回答1:


Handlers in JQCloud is supposed to be specified like this:

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

working example, http://jsfiddle.net/Q6348/7/




回答2:


A little different and maybe a easier way to implement it would be, to build your array like this:

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

This will give you the value of the node you clicked on.

Or if you want to pass it to a function, you can use it like this:

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

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

Hope this solution helps!




回答3:


you can always add a standard jquery handler for your click:

$(document).on('click', '#container-id .jqcloud-word', function() {
    var word_value = $(this).val();
    console.log(word_value);
});


来源:https://stackoverflow.com/questions/17037892/how-to-provide-a-click-handler-in-jqcloud

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!