简体   繁体   English

使jQuery Peity Bar Graph条成为可点击的

[英]Making jQuery Peity Bar Graph bars clickable

Is there a way to make the bars of a jQuery Peity bar graph clickable? 有没有一种方法可以使jQuery Peity条形图的条可点击?

I have created a 2 bar graph, and would like each bar to lead to a different link. 我创建了一个2条形图,希望每个条形都指向不同的链接。

The most basic method I can think of is not working: 我能想到的最基本的方法不起作用:

<span style='float:left;' <span style ='float:left;' id='" + id + "preProdBarGraph' class='preprodbar hidden'> <a class='button' href=' http://www.foo.com '>0</a>, <a class='button' href=' http://www.foo.com '>0</a> id ='“ + id +” preProdBarGraph'class ='隐藏了preprodbar'> <a class='button' href=' http://www.foo.com'> 0 </a>,<a class ='button 'href =' http://www.foo.com '> 0 </a>

And I am initializing the bar graph like such: 我正在初始化条形图,如下所示:

// set up the graph
jQuery(".preprodbar").peity("bar", {
      fill: ["#e71d32", "#FDD600", "#5AAAFA"]
})

Any help would be greatly appreciated! 任何帮助将不胜感激!

You can always use jquery's .on() handler to bind click events to any element to do anything. 您始终可以使用jquery的.on()处理函数将click事件绑定到任何元素以执行任何操作。

$(document).ready(function(){
    $(document).on('click',''#leftBar',function(){
         // do something
         window.location.href = 'http://www.mylink.com';
    });

    $(document).on('click',''#rightBar',function(){
         // do something
         window.location.href = 'http://www.mylink.com';
    });
});

I don't know how that graph structures its html, but if each bar is a direct child of the .preprodbar class you can always use the :nth-child(n) selector: 我不知道该图如何构造其html,但是如果每个条形图都是.preprodbar类的直接子级,则可以始终使用:nth-child(n)选择器:

$(document).ready(function(){
    $(document).on('click','.prepodbar :nth-child(1)',function(){
         // do something on the first child
         window.location.href = 'http://www.mylink.com';
    });

    $(document).on('click','.prepodbar :nth-child(2)',function(){
         // do something on the second child
         window.location.href = 'http://www.mylink.com';
    });
}); 

For the canvas element selection mentioned in the comments: 对于注释中提到的canvas元素选择:

$(document).on('click','canvas:eq(0)' ,function(){
     // do something
     $(this).css('background-color','#f00')
});

This selects the first <canvas> that you created. 这将选择您创建的第一个<canvas> Change the number in the :eq(0) to select any other, index is 0-based. 更改:eq(0)以选择其他任何数字,索引从0开始。 If you want to bind the event to all canvas elements, simply remove the :eq(0) substring from the selector. 如果要将事件绑定到所有画布元素,只需从选择器中删除:eq(0)子字符串。

Fiddle: http://jsfiddle.net/tjckobzp/20/ 小提琴: http : //jsfiddle.net/tjckobzp/20/

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

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