简体   繁体   English

如何使用raphael.js添加onclick事件以在一个容器中显示不同的文本?

[英]How can I add an onclick event with raphael.js to display different text in one container?

I am trying to create an onclick event for the svg donutchart. 我正在尝试为svg donutchart创建一个onclick事件。 Via default text1 should be displayed. 默认情况下,应显示text1。 By clicking another slice text1 should be swapped with the corresponding text. 通过单击另一个切片,text1应该与相应的文本交换。 The text section should sit besides the piechart. 文本部分应位于饼图旁边。

jsfiddle demo jsfiddle演示

slice6.click(function(setVisibility(id, visibility)){
    document.getElementById("text6").style.display = visibility;
});

Add the showText function to make it easier to show / hide a given text block. 添加showText函数以使其更容易显示/隐藏给定的文本块。

Then change the click listeners to the following. 然后将Click侦听器更改为以下内容。 You can't just pass any function to a click listener. 您不能只是将任何函数传递给Click侦听器。

You can either pass an anonymous function like this: 您可以传递这样的匿名函数:

e.click(function(e) {
  // I'm within an anonymous function
});

or you can create a function to pass to the click. 或者您可以创建一个函数以传递给点击。

function onclick(e) {
    // I'm a named function
}

e.click(onclick);

Note: If you don't need the event information, the function doesn't need to take argument e . 注意:如果不需要事件信息,则该函数不需要使用参数e

function showText(t1, t2, t3, t4, t5, t6) {
    document.getElementById("text1").style.display = (t1) ? "block" : "none";
    document.getElementById("text2").style.display = (t2) ? "block" : "none";
    document.getElementById("text3").style.display = (t3) ? "block" : "none";
    document.getElementById("text4").style.display = (t4) ? "block" : "none";
    document.getElementById("text5").style.display = (t5) ? "block" : "none";
    document.getElementById("text6").style.display = (t6) ? "block" : "none";
}

slice1.click(function(e) {
    showText(true, false, false, false, false, false);
});

slice2.click(function(e) {
    showText(false, true, false, false, false, false);
});

slice3.click(function(e) {
    showText(false, false, true, false, false, false);
});

slice4.click(function(e) {
    showText(false, false, false, true, false, false);
});

slice5.click(function(e) {
    showText(false, false, false, false, true, false);
});

slice6.click(function(e) {
    showText(false, false, false, false, false, true);
});

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

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