日期:2014-05-17  浏览次数:20856 次

在一个<a href=""></a>里实现2个不同链接
在一个<a href=""></a>里实现2个不同链接
当满足条件a时执行链接1,满足条件B时,执行条件2,怎么写脚本实现,谢谢各位了。。。

------解决方案--------------------
<a href="javascript:a();"></a>

function a() {
if (条件) {
location.href = 'url1';
} else {
location.href = 'url2';
}
}
------解决方案--------------------
<a href="javascript:void(0);" onClick="javascript:toNewUrl(‘result’, ‘newUrl’);">新链接</a>

/**
 * @param result 结果
 * @param newUrl 新URL地址
 */
function toNewUrl(result, newUrl) {
if (result) {
location.href = newUrl;
} else {
location.href = newUrl;
}


------解决方案--------------------
楼上两位方式都可以。
------解决方案--------------------
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function toUrl(result) {
  if (result=="baidu") {
    alert(result);
    location.href = "http://www.baidu.com";
  } else if  (result=="google"){
    alert(result);
    location.href = "http://www.google.com";
  }
}  
</script>
</head>
<body>
<a href="javascript:toUrl('google')" >google</a>
<a href="javascript:toUrl('baidu')" >baidu</a>
</body>
<html>

------解决方案--------------------
3楼的应该比较好
------解决方案--------------------
也可以使用jsp标签,进行判断