目前,我在我的站点中使用了这一小段js,让我的div充当一个按钮:
<div id="item" onclick="location.href='http://www.google.com';" style="cursor:pointer;">Google</div>但是在浏览网页时,我经常会打开大量的标签。我有没有办法修改我的代码来允许这一点呢?
发布于 2013-01-15 17:27:46
这应该可以做到:-
<html>
<head>
<style>
.sampleDiv
{
position:relative;
margin:0px auto;
width:400px;
height:200px;
background-color:#CCCCCC;
text-align:center;
}
.actualLink
{
position:absolute;
top:0px;
left:0px;
width:100%;
height:100%;
}
.linkText
{
position:relative;
top:80px;
}
</style>
</head>
<body>
<div class="sampleDiv">
<a class="linkText" href="test.html">testing</a>
<a class="actualLink" href="test.html"></a>
</div>
</body>
</html>与类actualLink的链接覆盖了整个div。linkText类的链接提供了文本。使用两个标记的目的是,如果您只使用actualLink,那么您不能将文本定位到使用带有linkText类的链接的任何位置want.By您可以灵活地(垂直地)将文本居中(水平居中只能使用actualLink)
发布于 2016-08-10 03:55:36
我的解决方案是用锚块替换div块。锚块现在可以采用几乎所有div可以做的CSS样式,但它还可以包含href,浏览器将识别href并为您提供想要查看的右键单击选项。
太老了:
<div class="divClass" onClick="location.href='http://www.google.com'">asdf</div>变成了
<a class="divClass" href="http://www.google.com">asdf</a>发布于 2012-09-21 19:47:51
你不能直接这样做,因为这是用户在浏览器上的设置,窗口是作为新窗口打开还是作为标签打开。
有target="_newtab",但没有得到广泛的支持。
所以在onclick中:
window.open('page.html','_newtab');但是,尝试覆盖用户的浏览器首选项并不是一个好主意。
要在鼠标右键上执行此操作,请执行以下操作:
$('#item').mousedown(function(event) {
if(event.which == 3) { // right click
window.open('page.html','_newtab');
}
})https://stackoverflow.com/questions/12529837
复制相似问题