简体   繁体   English

ChartJS 工具提示中的链接不可点击

[英]Link in ChartJS tooltip not clickable

I've added an <a> tag to my custom tooltip, but when I click on it it doesn't redirect to the href.我已经在我的自定义工具提示中添加了一个<a>标签,但是当我点击它时它不会重定向到 href。

I copied the custom tooltip exactly from these docs .我完全从这些文档中复制了自定义工具提示。 I'll also paste the code below for ease.为了方便起见,我还将粘贴下面的代码。

Then, I added the following innerHTML:然后,我添加了以下 innerHTML:

var tableRoot = tooltipEl.querySelector("table");
    tableRoot.innerHTML = innerHtml + '<a href="www.google.com">Click Here</a>';

However, the link isn't clickable - it doesn't even trigger cursor: pointer like a normal <a> tag.但是,该链接不可点击 - 它甚至不会触发cursor: pointer ,就像普通的<a>标签一样。

The full custom tooltip code:完整的自定义工具提示代码:

custom: function(tooltipModel) {
            // Tooltip Element
            var tooltipEl = document.getElementById('chartjs-tooltip');

            // Create element on first render
            if (!tooltipEl) {
                tooltipEl = document.createElement('div');
                tooltipEl.id = 'chartjs-tooltip';
                tooltipEl.innerHTML = '<table></table>';
                document.body.appendChild(tooltipEl);
            }

            // Hide if no tooltip
            if (tooltipModel.opacity === 0) {
                tooltipEl.style.opacity = 0;
                return;
            }

            // Set caret Position
            tooltipEl.classList.remove('above', 'below', 'no-transform');
            if (tooltipModel.yAlign) {
                tooltipEl.classList.add(tooltipModel.yAlign);
            } else {
                tooltipEl.classList.add('no-transform');
            }

            function getBody(bodyItem) {
                return bodyItem.lines;
            }

            // Set Text
            if (tooltipModel.body) {
                var titleLines = tooltipModel.title || [];
                var bodyLines = tooltipModel.body.map(getBody);

                var innerHtml = '<thead>';

                titleLines.forEach(function(title) {
                    innerHtml += '<tr><th>' + title + '</th></tr>';
                });
                innerHtml += '</thead><tbody>';

                bodyLines.forEach(function(body, i) {
                    var colors = tooltipModel.labelColors[i];
                    var style = 'background:' + colors.backgroundColor;
                    style += '; border-color:' + colors.borderColor;
                    style += '; border-width: 2px';
                    var span = '<span style="' + style + '"></span>';
                    innerHtml += '<tr><td>' + span + body + '</td></tr>';
                });
                innerHtml += '</tbody>';

                var tableRoot = tooltipEl.querySelector('table');
                tableRoot.innerHTML = innerHtml;
            }

            // `this` will be the overall tooltip
            var position = this._chart.canvas.getBoundingClientRect();

            // Display, position, and set styles for font
            tooltipEl.style.opacity = 1;
            tooltipEl.style.position = 'absolute';
            tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + 'px';
            tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + 'px';
            tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily;
            tooltipEl.style.fontSize = tooltipModel.bodyFontSize + 'px';
            tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle;
            tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
            tooltipEl.style.pointerEvents = 'none';
        }

Remove去掉

        tooltipEl.style.pointerEvents = 'none';

and any of theese from CSS以及 CSS 中的任何一个

        pointer-events: none;

I've faced the very problem myself, so I tried to remove this line:我自己也遇到过这个问题,所以我试图删除这一行:

tooltipEl.style.pointerEvents = 'none';

but it didn't work as expected - every time I hovered over the tooltip it disappeared.但它没有按预期工作 - 每次我将鼠标悬停在工具提示上时它都会消失。

Then, I tried this:然后,我尝试了这个:

  1. Keep the line above just as it is;保持上面的线不变;

  2. Modify events prop of the options .修改optionsevents属性。 (See docs ). (见文档)。 By doing this, you actually remove 'mouseout' event from the default config.通过这样做,您实际上从默认配置中删除了 'mouseout' 事件。

     const options = { maintainAspectRatio: false, events: ['mousemove', 'click', 'touchstart', 'touchmove'], plugins: { tooltip: { enabled: false, external: externalTooltipHandler }, legend: { display: false } }, }
  3. Then, remove pointer-events from the sole clickable element of the tooltip.然后,从工具提示的唯一可点击元素中删除指针事件。 In my case, it is li element.在我的例子中,它是li元素。 I did this with CSS. li { pointer-events: auto; }我用 CSS.li li { pointer-events: auto; } li { pointer-events: auto; }

When 1-3 steps are completed, the external tooltip is supposed to behave as expected AND the link elements contained in that tooltip should be clickable.完成 1-3 个步骤后,外部工具提示的行为应该符合预期,并且该工具提示中包含的链接元素应该是可点击的。

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

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