我有几个类型的表格单元格:
<td oncontextmenu=";return false;">
<a href="..."title="Hydrogen">H</a><br>
2.20
</td>我的问题是:如何制作一个javascript来捕获链接的标题,并在我右键单击相应的单元格时转到LINK?
发布于 2013-12-05 16:10:31
你要找的基本上是:
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中的第一个元素
发布于 2013-12-05 16:06:01
您可以使用window.location重定向用户。
<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);
}
}https://stackoverflow.com/questions/20404649
复制相似问题