onclick - plotly_click 不会触发
问题描述
按照此示例https://community.plot.ly/t/hyperlink-to-markers-on-map/17858/6,我试图使我的散点图可点击(具体来说,点击时打开网页)。单击数据点时如何触发单击事件?
这是一个 python turbogears 应用程序,plotly_click
事件发生在js
. 我正在使用plotly.js v1.44.1.
. 就像在示例中一样,我首先创建fig
:
plotlyHtml = webhelpers2.html.literal(plotly.offline.plot(fig, include_plotlyjs=True, output_type='div'))
然后我div
在图中找到了一个:
res = re.search('<div id="([^"]*)"', plotlyHtml)
div_id = res.groups()[0]
然后我构建一个js callback
要插入到script
模板中的标签中,如示例中所示。这是注入到 html 中的脚本标记:
<script type="text/javascript">
var plot_element = document.getElementById('3f56277f-e84b-4b68-ae85-91d3cd62d01c');
console.log('callback js has been added to page. did we get plot_element?');
console.log(plot_element);
plot_element.on('plotly_click', function(data){
console.log('Im inside the plotly_click!!');
console.log(data);
var point = data.points[0];
if (point) {
console.log(point.customdata);
window.open(point.customdata);
}
})
</script>
console.log
渲染页面时,事件侦听器之前的前两个语句会打印出来,这意味着js
成功注入html
. 特别是,plot_element
是一个带有class="plotly-graph-div js-plotly-plot"
. 但是,单击散点图上的点不会产生任何结果,不会产生任何错误、事件或控制台日志语句。我至少期望控制台日志语句if
会发生。我选择了错误的 div id 吗?
解决方案
问题出在我的layout
. 我有'clickmode': 'select'
,这把事情搞砸了。删除该启用事件按预期触发。
推荐阅读
- javascript - 在类中传递一个方法,该方法将参数传递给 pug 文件节点 js
- asp.net - .ashx TransmitFile pdf 未加载
- html - iOS css位置错误(位置:相对不适用于移动设备)
- typescript - 无法在 Typescript 中导入 svg.js
- php - 似乎无法让我的 php 代码正常工作
- hibernate - 独立模式导出 Hibernate Validator 集成
- python - 使用 super() 在基于类的视图中覆盖 get 方法
- r - 没有类型强制的数据框矩阵索引
- java - 创建线程范围/类范围对象的概念
- javascript - 在 React 中验证电子邮件的最简单和最短的方法是什么?