0

谁能告诉我为什么这两个 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>

上面的脚本在屏幕上(选取框)选取文本 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(选框)从页面中消失了。有什么建议么?

4

2 回答 2

3

两个脚本都在使用window.onload = something,所以第二个脚本阻止第一个脚本初始化。

在第一个中,删除 window.onload,并将第二个更改为:

function loadBothScripts()
{
    startticker();
    show2();
}

window.onload = loadBothScripts;
于 2012-06-29T15:06:29.727 回答
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);
于 2012-06-29T15:08:23.147 回答