我有几个类型的表格单元格:
<td oncontextmenu=";return false;">
<a href="..."title="Hydrogen">H</a><br>
2.20
</td>
我的问题是:如何制作一个捕获链接标题的javascript,当我在相应的单元格中单击鼠标右键时转到http://en.wikipedia.org/wiki/TITLE_OF_THE_LINK?
答案 0 :(得分:1)
您可以使用window.location
重定向用户。
<强> HTML 强>
<td oncontextmenu="gotoWiki(event);return false;">
<a href="..."title="Hydrogen">H</a><br>
2.20
</td>
<强> JS 强>
function gotoWiki(event) {
// Extract the target from the event
var target = event.target || event.srcElement;
// Get the link
var link;
if (target.tagName == "A") {
// If the target is an <a>-Tag, it's the link
link = target;
} else {
// Otherwise, get the first <a>-Tag
link = target.getElementsByTagName("a")[0];
}
// If getElementsByTagName() returned an element and it has the title attribute
if (link && title = link.getAttribute("title")) {
// Redirect
window.location.href = "http://en.wikipedia.org/wiki/" + encodeURIComponent(title);
}
}
答案 1 :(得分:0)
你正在寻找的基本上是这样的:
HTML:
<td oncontextmenu="goTitle(this); return false">
<a href="#" title="hydrogen">h</a>
</td>
JavaScript的:
function goTitle(el) {
var link = el.firstChild;
var url = "http://en.wikipedia.org/wiki/" + link.title;
window.location.href = url;
}
为简单起见,假设您的链接始终是td