单击对象内的区域时如何执行函数?(How to execute a function when clicking an area inside an object?)

编程入门 行业动态 更新时间:2024-10-27 00:27:03
单击对象内的区域时如何执行函数?(How to execute a function when clicking an area inside an object?)

我使用DOM-Elements创建了一个带有地图区域的对象。 现在我想在单击某个区域(坐标)时执行一个函数。 我想我也需要检查该区域是否被点击? 另外我觉得我在DOM-Element上缺少一些东西。

var _images = { //Object-MapArea start: { path: 'start.jpg', areas: [{ coords: '18,131,113,140', text: 'Homepage', onclick: doSomething, // ??? }] }, } // ..... there is more code not shown here //Creating DOM-Elements var areaElem = document.createElement('AREA'); // Set attributes areaElem.coords = area.coords; areaElem.setAttribute('link', area.goto); areaElem.setAttribute("title", area.text); areaElem.setAttribute("shape", "rect"); areaElem.onclick = doSomething; ? ? ? if (element.captureEvents) element.captureEvents(Event.CLICK); ? ? ? areaElem.addEventListener('click', onArea_click); _map.appendChild(areaElem); function doSomething(e) { if (!e) var e = window.event e.target = ... ? ? var r = confirm("Data will get lost!"); if (r == true) { window.open("homepage.html", "_parent"); } else { window.open(" ", "_parent"); // here I want to stay in the current pictures. I mean the current object map area. But how can I refer to it so it stays there ? } }

I created an object with map Areas by using DOM-Elements. Now I want to execute a function when clicked on an area(coordinates). I think I also Need to check the area if it is clicked or not ? Also I think I'm missing something on the DOM-Element.

var _images = { //Object-MapArea start: { path: 'start.jpg', areas: [{ coords: '18,131,113,140', text: 'Homepage', onclick: doSomething, // ??? }] }, } // ..... there is more code not shown here //Creating DOM-Elements var areaElem = document.createElement('AREA'); // Set attributes areaElem.coords = area.coords; areaElem.setAttribute('link', area.goto); areaElem.setAttribute("title", area.text); areaElem.setAttribute("shape", "rect"); areaElem.onclick = doSomething; ? ? ? if (element.captureEvents) element.captureEvents(Event.CLICK); ? ? ? areaElem.addEventListener('click', onArea_click); _map.appendChild(areaElem); function doSomething(e) { if (!e) var e = window.event e.target = ... ? ? var r = confirm("Data will get lost!"); if (r == true) { window.open("homepage.html", "_parent"); } else { window.open(" ", "_parent"); // here I want to stay in the current pictures. I mean the current object map area. But how can I refer to it so it stays there ? } }

最满意答案

查看我添加到您的代码中的评论;)

var _images = { //Object-MapArea
  start: {
    path: 'start.jpg',
    areas: [{
      coords: '18,131,113,140',
      text: 'Homepage',
      clickHandler: doSomething // I changed the name to avoid confusion but you can keep onclick 
    }]
  },
}

// ..... there is more code not shown here
//Creating DOM-Elements
var areaElem = document.createElement('AREA');

// Set attributes
areaElem.coords = area.coords;
areaElem.setAttribute('link', area.goto);
areaElem.setAttribute("title", area.text);
areaElem.setAttribute("shape", "rect");

// Add a listener on click event (using the function you defined in the area object above)
areaElem.addEventListener('click', area.clickHandler);

// Add your other click handler
areaElem.addEventListener('click', onArea_click);

_map.appendChild(areaElem);


function doSomething(e) {
  // Get the area clicked
  var areaClicked = e.target;

  // I dont understand the following code...
  var r = confirm("Data will get lost!");
  if (r == true) {
    window.open("homepage.html", "_parent");
  } else {
    window.open(" ", "_parent"); // here I want to stay in the current pictures. I mean the current object map area. But how can I refer to it so it stays there ?  
  }

} 
  
 

希望能帮助到你 ;)

See the comments I added to your code ;)

var _images = { //Object-MapArea
  start: {
    path: 'start.jpg',
    areas: [{
      coords: '18,131,113,140',
      text: 'Homepage',
      clickHandler: doSomething // I changed the name to avoid confusion but you can keep onclick 
    }]
  },
}

// ..... there is more code not shown here
//Creating DOM-Elements
var areaElem = document.createElement('AREA');

// Set attributes
areaElem.coords = area.coords;
areaElem.setAttribute('link', area.goto);
areaElem.setAttribute("title", area.text);
areaElem.setAttribute("shape", "rect");

// Add a listener on click event (using the function you defined in the area object above)
areaElem.addEventListener('click', area.clickHandler);

// Add your other click handler
areaElem.addEventListener('click', onArea_click);

_map.appendChild(areaElem);


function doSomething(e) {
  // Get the area clicked
  var areaClicked = e.target;

  // I dont understand the following code...
  var r = confirm("Data will get lost!");
  if (r == true) {
    window.open("homepage.html", "_parent");
  } else {
    window.open(" ", "_parent"); // here I want to stay in the current pictures. I mean the current object map area. But how can I refer to it so it stays there ?  
  }

} 
  
 

Hope it helps ;)

更多推荐

本文发布于:2023-07-29 20:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   函数   对象   区域   execute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!