Write an article to record my friends' methods to solve the problem. The function implemented by this JS is to click the tag. the IMG tag on the page displays the image corresponding to the tag link, the P label displays the text part of the label prompt.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
The following are examples of JavaScript code that can be implemented by friends.
Mubeibei
window.onload=prepareGallery;function prepareGallery(){ if (!document.getElementsByTagName ||!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++){ var source=links[i].getAttribute("href"); var text=links[i].getAttribute("title"); !function(i){ links[i].onclick=function(){ var img_holder=document.getElementById("imgholder"); var description=document.getElementById("description"); img_holder.setAttribute("src",this.href); description.firstChild.nodeValue=this.title; return false; } }(i) }}
T5500
window.onload=prepareGallery;function prepareGallery(){if (!document.getElementsByTagName ||!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(){ var img_holder=document.getElementById("imgholder");var description=document.getElementById("description");img_holder.src=,this.href;description.innerHTML=this.title;return false; } }}
Slowhand
window.onload=prepareGallery;function prepareGallery(){if (!document.getElementsByTagName ||!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++){var source=links[i].getAttribute("href");var text=links[i].getAttribute("title"); links[i].onclick=(function(img_src, img_txt){ return function(){ var img_holder=document.getElementById("imgholder"); var description=document.getElementById("description"); img_holder.setAttribute("src",img_src); description.firstChild.nodeValue=img_txt; return false; } })(source, text); }}
It can also run normally in the following format.
window.onload=prepareGallery;function prepareGallery(){if (!document.getElementsByTagName ||!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(){ var img_holder=document.getElementById("imgholder");var description=document.getElementById("description");img_holder.setAttribute("src",this.href);description.firstChild.nodeValue=this.title;return false; } }}
Idamag
Add attributes to each link so that each href and title value will be included in the source and TXT attributes.
window.onload=prepareGallery;function prepareGallery(){ if (!document.getElementsByTagName ||!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].source=links[i].getAttribute("href"); links[i].txt=links[i].getAttribute("title"); links[i].onclick=function(){ var img_holder=document.getElementById("imgholder"); var description=document.getElementById("description"); img_holder.setAttribute("src",this.source); description.firstChild.nodeValue=this.txt; return false; } }}
Use a closure to make links [I]. onclick is equal to the value of B returned by an anonymous function a. The value of source and text must be input to an anonymous function because of the closure (that is, the internal function can always obtain the variable of the peripheral function ), click the link and the internal function B is executed. When you find source and text, you can find the correct value.
window.onload=prepareGallery;function prepareGallery(){ if (!document.getElementsByTagName ||!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++){ var source=links[i].getAttribute("href"); var text=links[i].getAttribute("title"); links[i].onclick = (function(s,t){ return function(){ var img_holder=document.getElementById("imgholder"); var description=document.getElementById("description"); img_holder.setAttribute("src",s); description.firstChild.nodeValue=t; return false; } })(source,text) }}
Methods Used in books
window.onload=prepareGallery;function prepareGallery(){if (!document.getElementsByTagName ||!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(){showImg(this);return false;}}}function showImg(whichimg){var source=whichimg.getAttribute("href");var img_holder=document.getElementById("imgholder");img_holder.setAttribute("src",source);var text=whichimg.getAttribute("title");var description=document.getElementById("description");description.firstChild.nodeValue=text;}