javascript-关于图片页面跳转的问题

问题描述

关于图片页面跳转的问题
 初学JavaScript,在《JavaScript DOM编程艺术》书中有个实例代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
 <header>
      <img src="images/logo.gif" alt="Jay Skript and the Domsters" />
      <nav>
          <ul>
              <li><a href="index.html">Home</a></li>
              <li><a href="about.html">About</a></li>
              <li><a href="photos.html">Photos</a></li>
              <li><a href="live.html">Live</a></li>
              <li><a href="contact.html">Contact</a></li>
          </ul>
      </nav>
 </header>
 <article>
     <h1>Photos of the band</h1>
     <ul id="imagegallery">
        <li>
           <a href="images/photos/concert.jpg" title="The crowd goes wils">
              <img src="images/photos/thumbnail_concert.jpg" alt="the band in concert" />
           </a>
        </li>
        <li>
           <a href="images/photos/bassist.jpg" title="An atmospheric moment">
              <img src="images/photos/thumbnail_bassist.jpg" alt="the bassist" />
           </a>
        </li>
        <li>
           <a href="images/photos/guitarist.jpg" title="Rocking out">
              <img src="images/photos/thumbnail_guitarist.jpg" alt="the guitarist" />
           </a>
        </li>
        <li>
           <a href="images/photos/crowd.jpg" title="Encore! Encore! ">
              <img src="images/photos/thumbnail_crowd.jpg" alt="the audience" />
           </a>
        </li>
     </ul>
 </article>
<script>
function addLoadEvent(func) {

var oldonload = window.onload;
if (typeof window.onload != 'function') {
    window.onload = func;
} else {
    window.onload = function() {
        oldonload();
        func();
    }
  }
}

function insertAfter(newElement,targetElement) {

var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
} else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function showPic(pic) {
if (!document.getElementById("placeholder")) return false;
var source = pic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
if (placeholder.nodeName !="IMG") return false;
placeholder.setAttribute("src",source);
if (document.getElementById("description")) {

  var text = pic.getAttribute("title") ? pic.getAttribute("title") : "";
  var description = document.getElementById("description");
  if (description.firstChild.nodeType == 3) {
      description.firstChild.nodeValue = text;
     }
  }
  return true;

}

function prepareGallery() {

if (!document.getElementsByTagName) return false;
if (!document.getElementById) return false;
if (!document.getElementById("imagegallery")) return false;
var gallery = document.getElementById("imagegallery");
var links = gallery.getElementsByTagName("a");
for (var i=0; i<links.length; i++) {
    links[i].onclick = function() {
        return !showPic(this);
    }
  }
}
addLoadEvent(prepareGallery);

function preparePlaceholder() {
var placeholder = document.createElement("img");
placeholder.setAttribute("id", "placeholder");
placeholder.setAttribute("src", "image06/placeholder.gif");
placeholder.setAttribute("alt", "my image gallery");
var description = document.createElement("p");
description.setAttribute("id", "description");
var desctext = document.createTextNode("Choose an image.");
description.appendChild(desctext);
var gallery = document.getElementById("imagegallery");
insertAfter(placeholder, gallery);
insertAfter(description, placeholder);

}
addLoadEvent(preparePlaceholder);

</script>

</body>
</html>

单击缩略图,图片会在本页显示(不会跳转到另一页显示);
如果把showPic(pic)函数改写成如下:
function showPic(pic) {
if (!document.getElementById("placeholder")) return false;
var source = pic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
placeholder.setAttribute("src", source);
if (!document.getElementById("description")) return false;
if (pic.getAttribute("title")) {
    var text = pic.getAttribute("title");
} else {
    var text = "";
}
var description = document.getElementById("description");
if (description.firstChild.nodeType == 3) {
    description.firstChild.nodeValue = text;
}
return false;

}
单击图片缩略图,图片就会跳转到新的页面显示;实在找不出原因,请各位老师指教,非常感谢!

解决方案

 var description = document.getElementById("description");
if (description.firstChild.nodeType == 3) {
    description.firstChild.nodeValue = text;
}
///return false;
return true;///最后要return true,这样links[i].onclick = function() {        return !showPic(this);    } 连接点击后取反就是false,return false才是阻止连接跳转
时间: 2024-08-17 21:54:23

javascript-关于图片页面跳转的问题的相关文章

html5-请问JavaScript如何隐藏页面跳转时地址栏的参数

问题描述 请问JavaScript如何隐藏页面跳转时地址栏的参数 页面里不是form表单 button按钮的函数如下: setPass: function() { window.location.href="setpass.vue?tel=" + this.user.phone; } 每次点击按钮时跳转页面地址栏都有参数信息,请问用window.location.href 如何隐藏参数?谢谢 解决方案 get的话肯定是没办法隐藏参数,自己动态创建一个表单,表单method改为post,

javascript,ASP,HTML页面跳转全攻略

javascript|攻略|页面 =====javascript中弹出选择框跳转到其他页面=====<script language="javascript"><!--function logout(){if (confirm("你确定要注销身份吗?\n是-选择确定,否-选择取消")){window.location.href="logout.asp?act=logout"}}--></script> ====

javascript常用的页面跳转方法

先来介绍一下关于js的做法  代码如下 复制代码 function toUrl(ul){ if("" == ul || null==ul){          alert("感谢关注");       }else{                                     window.open(ul);//这是跳出新的窗口                  // window.location.href=ul;//这是直接在当前页跳转       

直接拿来用的页面跳转进度条JS实现_javascript技巧

本文实例介绍了基于javascript实现的页面跳转进度条,分享给大家供大家参考,具体内容如下 效果图: 具体代码: <HTML> <HEAD> <TITLE>open代码</TITLE> <SCRIPT type=text/javascript> <!-- var ie5 = (document.all && document.getElementsByTagName); var step = 0; function se

Angular 页面跳转时传参问题_javascript技巧

首先,你需要已经配置过你的rout,比如: $stateProvider .state('firstPage',{ url:'/Page/firstPage', templateUrl: 'Page/views/firstPage.html', controller: 'firstPageCtrl' //dependencies: ['service/vipSeachService'] }) .state('secPage', { params:{'message':null}, url: '/

JavaScript简单获取页面图片原始尺寸的方法_javascript技巧

本文实例讲述了JavaScript简单获取页面图片原始尺寸的方法.分享给大家供大家参考,具体如下: 这里通过Image()对象获取原始宽高 这种方式就没有那么麻烦,直接new一个Image()对象,然后把img的src赋值给他即可获取. var img = new Image(); img.src = $("#target").attr("src"); if(img.complete){ alert('width:'+img.width+',height'+img.

Javascript检查图片大小不要让大图片撑破页面_javascript技巧

如何用Javascript判断图片大小,其实只要写一个简单的函数就可以了,当然这么判断要怎么写很多人可能不知道.发觉用 ASP 判断图片大小比数牛毛还繁复, 且判断了就判断了, 还要逻辑显示...烦. 用 Javascript 判断, 轻松搞定, 最终效果无非就是不要让大图片撑破页面. <script LANGUAGE="JAVASCRIPT"> //检查图片大小是否大于预期大小, 大于则显示为预期大小 function show(chkw) { //chk images

javascript实现页面跳转的常用方法与代码实例

文章介绍了现在常用的页面跳转实现的方法包括了js php asp.net这三种的实现代码,有需要了解的朋友可以参考一下. 先来介绍一下关于js的做法  代码如下 复制代码 function toUrl(ul){ if("" == ul || null==ul){          alert("感谢关注");       }else{                                     window.open(ul);//这是跳出新的窗口    

Javascript检查图片大小不要让大图片撑破页面

 如何用Javascript判断图片大小,其实只要写一个简单的函数就可以了,当然这么判断要怎么写很多人可能不知道.发觉用 ASP 判断图片大小比数牛毛还繁复, 且判断了就判断了, 还要逻辑显示...烦. 用 Javascript 判断, 轻松搞定, 最终效果无非就是不要让大图片撑破页面.   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 3