有谁可以告诉我为什么这两个javascripts不会在同一页面上合作?
脚本#1
这就是头脑:
<!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" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
//<![CDATA[
<!--
var tWidth='600px'; // width (in pixels)
var tHeight='18px'; // height (in pixels)
var moStop=true; // pause on mouseover (true or false)
var fontfamily = 'arial,sans-serif'; // font for content
var tSpeed=7; // scroll speed (1 = slow, 5 = fast)
// enter your ticker content here (use \/ and \' in place of / and ' respectively)
var content='<a href="http:\/\/www.url.com\/">7679 - Reset Passwords<\/a>, ';
var cps=tSpeed; var aw, mq; var fsz = parseInt(tHeight) - 4; function startticker(){if (document.getElementById) {var tick = '<div style="position:relative;width:'+tWidth+';height:'+tHeight+';overflow:hidden;"'; if (moStop) tick += ' onmouseover="cps=0" onmouseout="cps=tSpeed"'; tick +='><div id="mq" style="position:absolute;left:0px;top:0px;font-family:'+fontfamily+';font-size:'+fsz+'px;white-space:nowrap;"><\/div><\/div>'; document.getElementById('ticker').innerHTML = tick; mq = document.getElementById("mq"); mq.style.left=(parseInt(tWidth)+10)+"px"; mq.innerHTML='<span id="tx">'+content+'<\/span>'; aw = document.getElementById("tx").offsetWidth; lefttime=setInterval("scrollticker()",50);}} function scrollticker(){mq.style.left = (parseInt(mq.style.left)>(-10 - aw)) ?parseInt(mq.style.left)-cps+"px" : parseInt(tWidth)+10+"px";} window.onload=startticker;
//-->
//]]>
</script>
这是脚本#1的主体:
<div id="ticker" align="center">
</div>
上面的脚本在屏幕(marquee)上对文本RTOL(从右到左)进行了标记。鼠标悬停停止选框。方便的小脚本,但问题是当我将下一个脚本实现到页面时它会中断...
脚本#2
这就是头脑:
<!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" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
//<![CDATA[
<!--
function show2(){
if (!document.all&&!document.getElementById)
return
thelement=document.getElementById? document.getElementById("tick"): document.all.tick
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()
var dn="PM"
if (hours<12)
dn="AM"
if (hours>12)
hours=hours-12
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
var ctime=hours+":"+minutes+":"+seconds+" "+dn
thelement.innerHTML=""+ctime+""
setTimeout("show2()",1000)
}
window.onload=show2
//-->
//]]>
</script>
这适用于脚本#2的主体:
<span id="tick"></span>
上面的脚本只显示当前机器时间,作为时钟工作,计算小时,分钟和秒。当我将此脚本添加到页面时,我的上述脚本#1(选取框)将从页面中消失。有什么建议吗?
答案 0 :(得分:3)
两个脚本都使用window.onload = something
,因此第二个脚本阻止了第一个脚本初始化。
在第一个中,删除window.onload,并将第二个更改为:
function loadBothScripts()
{
startticker();
show2();
}
window.onload = loadBothScripts;
答案 1 :(得分:1)
你的第一个剧本中有这一行:
window.onload = startticker;
然后你在第二个脚本中有这个:
window.onload = show2;
第二个覆盖了window.onload事件,因此第一个脚本不会触发。您可以使用此功能将多个处理程序附加到一个事件:
// addEventListener fix for IE
// o: object to which event listener is assigned
// e: event name as string
// f: the function
function myAddEventListener(o, e, f) {
if (o.addEventListener) { // standards compliant browsers
o.addEventListener(e, f, false);
} else if (o.attachEvent) { // IE8 and earlier
o.attachEvent("on" + e, f);
}
}
// replace all window.load = someFunction statements with this:
myAddEventListener(window, "load", startticker);
myAddEventListener(window, "load", show);