您的位置:首页 > 编程语言 > PHP开发

html中的a标签用js来实现页面跳转的功能(php分页时做的页面跳转)

2012-03-02 13:09 1181 查看
当php在做分页时,有时候会要想做个指定跳转到某页的功能,而要跳转到某页就要涉及到js。因为必须通过js来找到用户输入的是什么,是哪一页。

所以,如果你用的是一个图片来做点击实现跳转的话,那么就可以通过在图片外面包裹一个a标签来实现点击并获取输入值。

  现在问题是这样写:

<a href="" onclick=“page()”>那么浏览器中是不能实现跳转的呢。因为page()的js方法中不管你怎么写Windows.location.href="www.zhishuibei.com";都不能跳转,原因是a标签的href里面是空的,浏览器执行了这个地方。那么怎么办呢?

  其实写程序久了就容易忘了html里面的一些常识。呵呵。那么问题怎么解决呢?看下面:

<a href="javascript:page();">现在去试试呢。或者:

<a href="javascript:;" onclick="page()">

也去试试呢。没有浏览器兼容的问题吧。呵呵。就这么简单。只是因为忘了一些HTML常识而已。

下面是我的页面源码,共享出来给大家:

blog.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--

Design by Free CSS Templates http://www.freecsstemplates.org Released for free under a Creative Commons Attribution 2.5 License

Title : Simpleton
Version : 1.0
Released : 20110315
Description: A two-column web design, best for your personal and business blogging.

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>GayaYang的个人博客---首页</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="/default.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="/css/xp1980.css" />
<script type="text/javascript" src="/css/xp1980.js"></script>
<style type="text/css">
/*
a{color:blue}
body { margin: 0; padding: 0;
/*font: 10px normal Verdana, Arial, Helvetica, sans-serif;
background: #fff; }
img {border: none;}
ul,li{list-style: none;}
*/
#menu_out{width:964px;background:url(/images/menu_left.gif) no-repeat left top;margin:5px auto;}
#menu_in{background:url(/images/menu_right.gif) no-repeat right top;padding-right:4px;}
#menu{height:46px;}
#menu ul{padding:0;border:0;list-style:none;line-height:150%;margin-left:4px;}
#topnav ul{font-size: 14px;float: left;}
#topnav li{width:119px; overflow: hidden; float: left; height:46px;}
#topnav a, #topnav span {width:119px;padding:11px 0;text-align:center;float: left; text-decoration: none; color: #fff;text-transform: uppercase;clear: both;height: 24px;line-height: 24px;font-size: 14px;}

#topnav span{background: url(/images/a_bg.gif) repeat-x left top;}
#topnav a{color: #000;background: url(/images/a_bg.gif) repeat-x left bottom;}

#pages{
border:1px solid #939393;
font-size:14px;
margin:5px 5px;
padding:1px 8px;
}
#pagea a:hover{
background-color:#103C5D;
color:white;
}
</style>
<script type="text/javascript" src="/images/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#topnav li").prepend("<span></span>"); //Throws an empty span tag right before the a tag
$("#topnav li").each(function() { //For each list item...
var linkText = $(this).find("a").html(); //Find the text inside of the a tag
$(this).find("span").show().html(linkText); //Add the text in the span tag
});

$("#topnav li").hover(function() { //On hover...
$(this).find("span").stop().animate({
marginTop: "-46" //Find the span tag and move it up 40 pixels
}, 250);
} , function() { //On hover out...
$(this).find("span").stop().animate({
marginTop: "0" //Move the span back to its original state (0px)
}, 250);
});
});
</script>
</head>
<body>
<!-- start header -->
<div id="header">
<div style="width:960px;text-align:right;">
<ul>
<li><a href="/index.php/login/loginblog" style="font:10px Consolas,normal Helvetica, Arial, sans-serif;">后台登录 | </a></li>
<li><a href="#" style="font:10px Consolas,normal Helvetica, Arial, sans-serif;">版权申明 | </a></li>
<li><a href="#" style="font:10px Consolas,normal Helvetica, Arial, sans-serif;">关于我 </a></li>
</ul>
</div>
<div id="logo">
<div style="font-size:50px;font-family:Consolas,normal Helvetica, Arial, sans-serif;"><a href="#" style="color:#0F2E5C">GayaYang's personal blog </a></div>
<a href="http://www.zhishuibei.com/" style="font:14px Consolas,normal Helvetica, Arial, sans-serif bloder">In the day with the wind, go up in the world.</a>
</div>

<div style="width:960px;text-align:right;">

<!-- 导航条部分开始(拷贝过来的很不错的样式) -->
<div id=menu_out>
<div id=menu_in>
<div id="menu">
<ul id="topnav">
<?php
if($menu){
foreach ($menu as $row){
?>
<li><a href="<?php echo $row->Path;?>" style="clear:both;"><?php echo $row->Name;?></a></li>
<?php
}
}
?>
</ul>
</div>
</div>
</div>
<!-- 导航条部分结束(拷贝过来的很不错的样式) -->
</div>
</div>
<!-- end header -->
<!-- start page -->
<div id="wrapper">
<div id="page">
<!-- start content -->
<div id="content">
<?php
if(!empty($datas)){
$_month=array('01'=>'Jan','02'=>'Feb','03'=>'Mar','04'=>'Apr','05'=>'May','06'=>'Jun','07'=>'Jul','08'=>'Aug','09'=>'Sep','10'=>'Oct','11'=>'Nov','12'=>'Dec');
foreach ($datas as $row){
$month = substr($row->Date, 5,2);
$day = substr($row->Date, 8,2);
$month = $_month[$month];
$content = $row->Content;
if(strlen($row->Content)>500){
//echo htmlspecialchars($content);
$content = mb_substr($row->Content,0,500,'utf-8').'......';
//因为后来加入了代码高亮显示的功能插件,所以在截取字符串时要考虑是否截断了高亮显示的代码部分,如果截断了就会导致页面乱了。
if(strpos($content, 'class="UGCode"')){
//$pos = strpos($content, '<div');
$content = $row->Content;
}
//下面的方法是不用mb系列的函数而是调用model层自己写的函数来操作截取中文字符(不处理会出现中文乱码现象)
//$content = $this->index_model->getstr($row->Content,300,'utf-8');
//$content = $content.'......';
}
?>
<div class="post" style="border-bottom: 1px solid #CCCCCC;margin:50px auto;">
<p class="date"><?php echo $month;?><b><?php echo $day;?></b></p>
<h2 class="title"><a href='/index.php/blog/blog_detail/<?php echo $row->ID;?>'><?php echo stripslashes($row->Title);?></a></h2>
<p class="meta"><small>Author: <a href="#"><?php echo $row->Author;?></a> | Date: <?php echo $row->Date;?>, 评论:<?php echo $row->Comment;?> | 转发:<?php echo $row->Transpublish;?> »</small></p>
<div class="entry">
<?php echo stripslashes($content);?>
</div>
</div>
<?php
}
}
?>

<table>
<tr>
<td height="29">
<?php
if(!empty($datas)){
if($total_rows%$limit==0){
$num = $total_rows/$limit;
}else{
$num = intval($total_rows/$limit)+1;
}
if(empty($offset))
$page=1;
else
$page=$offset/$limit+1;
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15%" height="29" nowrap="nowrap"><span style="font-size:12px;">共<?php echo $total_rows;?>条纪录,每页<?php echo $limit;?>条纪录</span></td>
<td width="85%" valign="top" class=""><div align="right">
<table height="20" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="62" height="22" valign="middle"><div align="right">
<?php if($page>1){
?>
<a href=<?php echo "/index.php/blog/index/0/".$limit."/";?>><img src="/tab/images/first.gif" width="37" height="15" /></a>
<?php
}else{
?>
<img src="/tab/images/first.gif" width="37" height="15" />
<?php
}
?>
</div>
</td>
<td width="50" height="22" valign="middle">
<div align="right">
<?php if($page>1){
?>
<a href=<?php echo "/index.php/blog/index/".(($page-2)*$limit)."/".$limit."/";?>><img src="/tab/images/back.gif" width="43" height="15" /></a>
<?php
}else{
?>
<img src="/tab/images/back.gif" width="43" height="15" />
<?php
}
?>
</div>
</td>
<td width='50' height='22' valign='middle'>
<div align="right">
<?php
// for ($i = 1; $i < $num+1; $i++) {
// echo "<span id='pagea'><a id='pages' href='/index.php/blog/index/".(($i-1)*$limit)."/".$limit."'".">".$i."</a></span>";
// }
if ($num > $pageNumber) {//控制如果超过了显示的页码数量,就只显示规定数量的。例如总共分了50个页面,而50个页面全部显示出来肯定页面就乱了,所以就规定只显示10个页码或者8个5个什么的。
if(!empty($page)){
$index = $page;
}else{
$index = 1;
}
for ($i = $index; $i < $index+$pageNumber/2-1; $i++) {
if($i>$num)
break;
echo "<span id='pagea'><a id='pages' href='/index.php/blog/index/".(($i-1)*$limit)."/".$limit."'".">".$i."</a></span>";
}
}
else{
for ($i = 1; $i < $num+1; $i++) {
echo "<span id='pagea'><a id='pages' href='/index.php/blog/index/".(($i-1)*$limit)."/".$limit."'".">".$i."</a></span>";
}
}
?>
</div>
</td>
<!-- <td width="150" height="22"><?php //echo $pagination;?></td>-->
<td width="54" height="22" valign="middle">
<div align="right">
<?php if($page<$num){
?>
<a href=<?php echo "/index.php/blog/index/".($page*$limit)."/".$limit."/";?>><img src="/tab/images/next.gif" width="43" height="15" /></a>
<?php
}else{
?>
<img src="/tab/images/next.gif" width="43" height="15" />
<?php
}
?>
</div></td>
<td width="49" height="22" valign="middle">
<div align="right">
<?php if($page<$num){
?>
<a href=<?php echo "/index.php/blog/index/".(($num-1)*$limit)."/".$limit."/";?>><img src="/tab/images/last.gif" width="37" height="15" /></a>
<?php
}else{
?>
<img src="/tab/images/last.gif" width="37" height="15" />
<?php
}
?>
</div></td>
<td width="59" height="22" valign="middle"><div align="right" style="font-size:12px;">转到第</div></td>
<td width="25" height="22" valign="middle"><span class="STYLE7">
<input name="textfield" id="go" type="text" class="STYLE1" style="height:10px; width:25px;" size="5" />
</span></td>
<td width="23" height="22" valign="middle" style="font-size:12px;">页</td>
<td width="30" height="22" valign="middle"><a href="javascript:gotoPage();"><img src="/tab/images/go.gif" width="37" height="15" /></a></td>
</tr>
</table>
</div></td>
</tr>
</table>
<?php
}
?>
</td>
</tr>
</table>
<script type="text/javascript">
function gotoPage(){
var go = document.getElementById('go').value;
var limit = <?php echo $limit;?>;
window.location.href = "/index.php/blog/index/"+(go-1)*limit+"/"+limit;
//alert(window.location.href);
}
</script>
</div>
<!-- end content -->
<!-- start sidebar -->
<?php include 'sidebar.php';?>
<!-- end sidebar -->
<br style="clear: both;" />
</div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">
<p id="legal"> ©2011 Simpleton . All Rights Reserved.
  •  
Design by <a href="http://www.freecsstemplates.org/">Free CSS Templates</a>   •  
Icons by <a href="http://famfamfam.com/">FAMFAMFAM</a>. <a href="http://validator.w3.org/check/referer" class="xhtml" title="This page validates as XHTML">Valid <abbr title="eXtensible HyperText Markup Language">XHTML</abbr></a>   •   <a href="http://jigsaw.w3.org/css-validator/check/referer" class="css" title="This page validates as CSS">Valid <abbr title="Cascading Style Sheets">CSS</abbr></a> </p>
<!-- end footer -->
</div>
</body>
</html>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息