小弟琢磨这个特效很久了、在IE内核的浏览器图片就显示,而且点击大图也可以点击进去,唯独火狐、苹果、谷歌浏览器就不显示图片,而且点击图片就会显示对象为空,希望高手帮修改一下兼容问题,小弟对JavaScript一窍不通.....能帮得上忙的就帮帮一下、小弟感激不尽!进来看了不能帮得上忙的,在此也谢过了!
我觉得问题出现在“$”这个符号,但是不知道怎么改
以下是代码:
<table width="790" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td colspan="2" align="left" class="txt_1"></td>
</tr>
<tr>
<td width="520" align="center"><img src="images/1.jpg" width="520" height="410" border="0" id="main_img" rel="#" link="#" /></td>
<td width="110" align="center" valign="top">
<img src="images/gotop.gif" width="100" height="14" id="gotop" />
<div id="showArea">
<img src="images/01.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/01.jpg" link=" #" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/02.jpg" link="#" />
<img src="images/03.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/03.jpg" link="#" />
<img src="images/01.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/01.jpg" link="#" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/02.jpg" link="#" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/03.jpg" link="#" />
<img src="images/01.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/01.jpg" link="#" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/02.jpg" link="#" />
<img src="images/03.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/03.jpg" link="#" />
<img src="images/01.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/01.jpg" link="#" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/02.jpg" link="#" />
<img src="images/03.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/03.jpg" link="#" />
<img src="images/gobottom.gif" width="100" height="14" id="gobottom" /></td>
</tr>
</table>
<script language="javascript" type="text/javascript">
function $(e) {return document.getElementById(e);}
document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
}
var MyMar;
var speed1 = 1; //速度,越大越慢
var spec1 = 1; //每次滚动的间距, 越大滚动越快
var ipath = 'images/'; //图片路径
var thumbs = document.getElementsByClassName('thumb_img');
for (var i=0; i<thumbs.length; i++) {
thumbs[i].onmouseover = function () {$('main_img').src=this.rel; $('main_img').link=this.link;};
thumbs[i].onclick = function () {location = this.link}
}
$('main_img').onclick = function () {location = this.link;}
$('gotop').onmouseover = function() {this.src = ipath + 'gotop2.gif'; MyMar=setInterval(gotop,speed1);}
$('gotop').onmouseout = function() {this.src = ipath + 'gotop.gif'; clearInterval(MyMar);}
$('gobottom').onmouseover = function() {this.src = ipath + 'gobottom2.gif'; MyMar=setInterval(gobottom,speed1);}
$('gobottom').onmouseout = function() {this.src = ipath + 'gobottom.gif'; clearInterval(MyMar);}
function gotop() {$('showArea').scrollTop-=spec1;}
function gobottom() {$('showArea').scrollTop+=spec1;}
</script>
我觉得问题出现在“$”这个符号,但是不知道怎么改
以下是代码:
<table width="790" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td colspan="2" align="left" class="txt_1"></td>
</tr>
<tr>
<td width="520" align="center"><img src="images/1.jpg" width="520" height="410" border="0" id="main_img" rel="#" link="#" /></td>
<td width="110" align="center" valign="top">
<img src="images/gotop.gif" width="100" height="14" id="gotop" />
<div id="showArea">
<img src="images/01.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/01.jpg" link=" #" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/02.jpg" link="#" />
<img src="images/03.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/03.jpg" link="#" />
<img src="images/01.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/01.jpg" link="#" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/02.jpg" link="#" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/03.jpg" link="#" />
<img src="images/01.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/01.jpg" link="#" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/02.jpg" link="#" />
<img src="images/03.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/03.jpg" link="#" />
<img src="images/01.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/01.jpg" link="#" />
<img src="images/02.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/02.jpg" link="#" />
<img src="images/03.jpg" width="80" height="50" border="0" class="thumb_img" rel="images/03.jpg" link="#" />
<img src="images/gobottom.gif" width="100" height="14" id="gobottom" /></td>
</tr>
</table>
<script language="javascript" type="text/javascript">
function $(e) {return document.getElementById(e);}
document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
}
var MyMar;
var speed1 = 1; //速度,越大越慢
var spec1 = 1; //每次滚动的间距, 越大滚动越快
var ipath = 'images/'; //图片路径
var thumbs = document.getElementsByClassName('thumb_img');
for (var i=0; i<thumbs.length; i++) {
thumbs[i].onmouseover = function () {$('main_img').src=this.rel; $('main_img').link=this.link;};
thumbs[i].onclick = function () {location = this.link}
}
$('main_img').onclick = function () {location = this.link;}
$('gotop').onmouseover = function() {this.src = ipath + 'gotop2.gif'; MyMar=setInterval(gotop,speed1);}
$('gotop').onmouseout = function() {this.src = ipath + 'gotop.gif'; clearInterval(MyMar);}
$('gobottom').onmouseover = function() {this.src = ipath + 'gobottom2.gif'; MyMar=setInterval(gobottom,speed1);}
$('gobottom').onmouseout = function() {this.src = ipath + 'gobottom.gif'; clearInterval(MyMar);}
function gotop() {$('showArea').scrollTop-=spec1;}
function gobottom() {$('showArea').scrollTop+=spec1;}
</script>
//rel、link都不是img元素的标准属性,属于自定义属性,必须使用setAttribute()方法修改、getAttribute()方法获取值
for (var i = 0; i < thumbs.length; i++) {
thumbs[i].onmouseover = function() {
$('main_img').src = this.getAttribute('rel');
$('main_img').setAttribute('link', this.getAttribute('link'));
};
thumbs[i].onclick = function() {
location = this.getAttribute('link');
}
}
$('main_img').onclick = function() {
location = this.getAttribute('link');
}
请到网上下载jquery,用
<script type="text/javascript" src="../jquery.js"></script>
方式加载到页面中,注意不要弄错路径但如果没有加载jquery的话,应该会报对象或成员未定义的脚本错误啊
所以可能jquery没有问题,楼主看一下应该有包含jquery.js的,目测问题在:
for (var i=0; i<thumbs.length; i++) {
thumbs[i].onmouseover = function () {$('main_img').src=this.rel; $('main_img').link=this.link;};
thumbs[i].onclick = function () {location = this.link}
}
$('main_img')这种写法应该是按元素名称,但实际没有main_img这种html元素吧,所以报对象为空?
但这应该是在鼠标进入时报啊。
把那两个地方改为$('#main_img')试试,这种写法是按id选择,就不会为空了