【问题标题】:Can't properly rebind imagemapster to set new tooltips (TypeError: Cannot read property '0' of null)无法正确重新绑定 imagemapster 以设置新的工具提示(TypeError:无法读取属性 '0' of null)
【发布时间】:2019-08-27 20:35:09
【问题描述】:

我正在尝试为当地游泳俱乐部的可出租储物柜创建解决方案。每个更衣室的数据(大约 1800 个)是从 SQL 数据库生成的,我分别映射了每个更衣室。 当我尝试重新绑定我的图像映射时,就会出现问题。我希望用户能够翻阅这些年,并查看在特定年份租用了哪个储物柜。

为了让用户有一个更好的概览,我用红色或绿色填充了每个储物柜区域,因为背景图像显示了每个储物柜的编号(名称),所以不透明度很低。工具提示显示更衣室和号码、会员全名、最后付款日期、更衣室是否可用以及评论字段。

但是当用户更改年份,并且我尝试为每个单独的储物柜设置新的工具提示和可能的新颜色时,我收到以下错误: "TypeError: 无法读取 null 的属性 '0'"

此错误只会阻止工具提示显示。点击区域仍然会将用户带到正确的储物柜,并且点击事件具有正确的数据、正确的年份、正确的会员、正确的付款日期等。

我尝试了“unbind”、“rebind”、“set_options”的几种组合,但都不走运。

要么我设法将工具提示更新为正确的值,但只是再次应用颜色,这导致在几次切换后出现一堆黑色或红色方块(没有抛​​出错误),或者我完全重新着色并且工具提示消失了。尝试重新使用 via .attr("usemap" , "#mapId") oder .useMap = "mapId" 也没有用。

一些 HTML

            <img id="room0img" src="\assets\LockerLayouts\female1.PNG" usemap="#room0map" [hidden]="!visibility[0]" width="1230px">

                <map name="room0map" id="room0map" [hidden]="!visibility[0]">

                    <area alt="365" name="365" coords="22,167,105,193" shape="rect" href="#">
                    <area alt="364" name="364" coords="22,196,105,221" shape="rect" href="#">
                    <area alt="363" name="363" coords="22,224,105,250" shape="rect" href="#">

我的 .ts 函数尝试重新应用/重新绑定/无论如何,以当前形式显示正确的颜色,但会杀死工具提示。它被调用 onInit 并且当用户更改他想要查看/编辑的年份时。在每次调用之前设置变量并使用相应年份的正确数据

if (this.initialLoad === true){
  $('#room' + this.roomId + 'img').mapster(this.basic_opts);
} else {

  $('#room' + this.roomId + 'img').mapster();
  $('#room' + this.roomId + 'img').attr("usemap", '#room' + this.roomId + 'map');
  $('#room' + this.roomId + 'img').mapster( "rebind" ,this.basic_opts);
}
  $('#room' + this.roomId + 'img').mapster('set', this.rented, this.unrentedLockers , this.unrented_opts);
  $('#room' + this.roomId + 'img').mapster('set', this.rented, this.unrentedBrokenLockers , this.unrentedBroken_opts);
  $('#room' + this.roomId + 'img').mapster('set', this.rented, this.rentedPaidLockers , this.rentedPaid_opts);
  $('#room' + this.roomId + 'img').mapster('set', this.rented, this.rentedPaidBrokenLockers , this.rentedPaidBroken_opts);
  $('#room' + this.roomId + 'img').mapster('set', this.rented, this.rentedUnpaidLockers , this.rentedUnpaid_opts);
  $('#room' + this.roomId + 'img').mapster('set', this.rented, this.rentedUnpaidBrokenLockers , this.rentedUnpaidBroken_opts);

工具提示本身是在名为 onConfigured 的不同函数中创建的。如果我只是重新应用选项,它们会正确更新,但颜色会相互叠加。我 console.log() 'ed 基本上一切。数据在那里,只是没有链接到图像地图。区域ID是根据储物柜的名称设置的。当我重新绑定 .mapster 时,数据会丢失,我无法重新应用它。但正如我所说,只有工具提示丢失了它们的链接,onClick() 事件仍然会产生正确的结果,即链接到允许用户编辑储物柜的不同页面。

完全错误:

TypeError: Cannot read property '0' of null
core.js:7187
message:"Cannot read property '0' of null"
stack:"TypeError: Cannot read property '0' of null\n    
at u.MapData.getAllDataForArea (http://localhost:4200/scripts.js:18:25591)\n    
at HTMLAreaElement.r (http://localhost:4200/scripts.js:18:22656)\n    
at HTMLAreaElement.n.mouseover (http://localhost:4200/scripts.js:18:24547)\n    
at HTMLAreaElement.dispatch (http://localhost:4200/scripts.js:2:42571)\n    
at HTMLAreaElement.v.handle (http://localhost:4200/scripts.js:2:40572)\n    
at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:3240:31)\n    
at Object.onInvokeTask (http://localhost:4200/vendor.js:68456:33)\n    
at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:3239:60)\n    
at Zone.runTask (http://localhost:4200/polyfills.js:3017:47)\n    
at ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:3314:34)"

属性会根据鼠标悬停的区域而变化,但它不是“正确”的数字。如果我在初始页面加载后将鼠标悬停在储物柜 365 上,并且它显示正确的数字,我在年份更改后悬停在它上面并显示 0,我认为这是因为它是我的 html 文件中的第一个。

【问题讨论】:

    标签: javascript jquery html angular imagemapster


    【解决方案1】:

    在再次阅读纪录片后(......一次又一次......)我终于成功实现了我想要的!

    我认为我使用了 imagemapster “错误”:我通过选择区域为这些区域着色

    我只需将它们全部取消选择,然后就可以使用正确的颜色和工具提示重新绑定它们。我还必须从选项的“onConfigured”属性中删除“setToolTips()”,并在应用/重新绑定后调用它

    if (this.initialLoad === true){
      $('#room' + this.roomId + 'img').mapster(this.basic_opts)
      this.setToolTips()
    } else {
      $('#room' + this.roomId + 'img').mapster("deselect");
      $('#room' + this.roomId + 'img').mapster( "rebind" ,this.basic_opts);
      this.setToolTips()
    
    }
      $('#room' + this.roomId + 'img').mapster('set', this.rented, this.unrentedLockers , this.unrented_opts);
      $('#room' + this.roomId + 'img').mapster('set', this.rented, this.unrentedBrokenLockers , this.unrentedBroken_opts);
      $('#room' + this.roomId + 'img').mapster('set', this.rented, this.rentedPaidLockers , this.rentedPaid_opts);
      $('#room' + this.roomId + 'img').mapster('set', this.rented, this.rentedPaidBrokenLockers , this.rentedPaidBroken_opts);
      $('#room' + this.roomId + 'img').mapster('set', this.rented, this.rentedUnpaidLockers , this.rentedUnpaid_opts);
      $('#room' + this.roomId + 'img').mapster('set', this.rented, this.rentedUnpaidBrokenLockers , this.rentedUnpaidBroken_opts);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-24
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多