简体   繁体   English

单击鼠标时打开链接

[英]Open link when mouse is clicked

I am very new to Javascript so I don't know too much. 我是Java的新手,所以我不太了解。 I want the user to click this shape and for it to open a link. 我希望用户单击此形状并为其打开链接。 Here is my code: 这是我的代码:

stage.on('mousedown', function(OpenLink) {
    var shape = evt.targetNode;
    if (shape) {
        Application.OpenURL("[My link goes here]");
    }
  });

I don't know if this is even close to being right, but I can't seem to find an answer. 我不知道这是否接近正确,但我似乎找不到答案。

EDIT: This is code that I have added onto this: http://www.html5canvastutorials.com/labs/html5-canvas-interactive-building-map/ 编辑:这是我添加到此的代码: http : //www.html5canvastutorials.com/labs/html5-canvas-interactive-building-map/

Use window.location = "[My link goes here]"; 使用window.location = "[My link goes here]"; to navigate to another url. 导航到另一个URL。

I am assuming you have jQuery installed, considering you've included the jQuery tag in your question. 考虑到您已在问题中包含jQuery标签,因此我假设您已安装jQuery。 If not, see http://jquery.com 如果没有,请访问http://jquery.com

You can do this via a jQuery click event handler: 您可以通过jQuery click事件处理程序执行此操作:

$('.stage').mousedown(function(e) {
    var shape = e.target;
    window.location.href = 'http://example.com';
});

If this is not what you were looking for, please reply to this answer explaining what you do want it to do. 如果这不是您想要的内容,请回复此答案,以解释您希望它做什么。

See demo . 参见演示

PS: in this script you don't do anything with the element that triggered the click event, but you do store it in the shape variable. PS:在此脚本中,您不会对触发click事件的元素进行任何操作,但会将其存储在shape变量中。 I included this purely to demonstrate how to get the element that triggered the event, but in this script, that line can be omitted. 我仅包括此内容是为了演示如何获取触发事件的元素,但是在此脚本中,该行可以省略。

Think this should work. 认为这应该工作。 'stage' assumes you're referencing the object tag. 'stage'假设您正在引用对象标签。 If 'stage' is a class or id, would need to update accordingly. 如果“ stage”是类或ID,则需要相应地进行更新。

$('stage').mousedown(function(ev){    // Attach mousedown listener
  if (ev.target.targetNode) {   // if clicked object has 'targetNode'
    ev.preventDefault();   // prevent additional bubbling
    window.location.href = 'http://www.google.com';    // Send browser to url
  }
});

You could also store the url on the clicked objects as an attribute or data, and grab it when clicked. 您还可以将URL作为属性或数据存储在单击的对象上,并在单击时获取它。

<thing data-myUrl="www.website.com"></thing>

.. and: ..和:

window.location.href = $(ev.target).data('myUrl')

EDIT: 编辑:

Using the functions from the link you provided, your function should be: 使用提供的链接中的功能,您的功能应为:

stage.on('mousedown', function(evt) {
  var shape = evt.targetNode;
  if (shape) {
    window.location.href = 'http://www.google.com';
  }
});

.. so this will send the browser to your url, anytime the user clicks on anything that has ANYTHING as its 'shape' value. ..因此,只要用户单击任何具有“形状”值的东西,它就会将浏览器发送到您的网址。 You could use (shape === 'circle') or whatever if you wanted to be more specific about what they clicked on. 您可以使用(shape === 'circle')或其他任何想要更具体地了解他们单击的内容的方法。 ('circle' is a guess, your actual shape values may differ) (“圆圈”是一个猜测,您的实际形状值可能有所不同)

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

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