事件处理程序使用

时间:2011-01-18 05:19:31

标签: javascript events handler

是否有办法在自动脚本完成时执行某个功能,而不是为了响应用户的操作而启动该功能,例如鼠标移动?

3 个答案:

答案 0 :(得分:0)

只需在脚本末尾调用该函数即可。 nameoffunction()

答案 1 :(得分:0)

function nameoffunction(){
    //code in this function will be done after the window loads
    alert("hi");
}

window.onload=function(){
    var alignarray=['left','center','right']; 
    var elem=document.getElementById('text-content');
    elem.style.textAlign=alignarray[Math.round(Math.random()*2)]; 
    nameoffunction();
}

答案 2 :(得分:0)

我刚修改了你的代码。

<font face="helvetica" color="1b1b1b" size="5px" repeat>

<html>
<head>
<style type="text/css">
#text-box {

padding: 4px;
width: 602px;
}
#text-content {
color: #1b1b1b;

}
</style>

</head>
<body>

<div id="text-box">
<div id="text-content"></div>

</div>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

$(document).ready( function() {
var textArray = [
'Flow',
'Precision',
'Voice',
'Imagery',
'Pace',
'Unity',
'Word Choice',
'Rhythm',
'Inspiration',
'Balance',
'Clarity',
'Simplicity',
'Revision',
'Discipline',
'Fundamentals',
'Dedication',
'Practice',
];
$('#text-content').loadText( textArray, 5500 ); // ( array, interval )
});

// custom jquery plugin loadText()
$.fn.loadText = function( textArray, interval ) {
return this.each( function() {
var obj = $(this);
obj.fadeOut( 'slow', function() { 
obj.empty().html( random_array( textArray ) );
obj.fadeIn( 'slow' );
alignarray()
});

timeOut = setTimeout( function(){ obj.loadText( textArray, interval )}, interval );

});
}
//public function
function random_array( aArray ) {
var rand = Math.floor( Math.random() * aArray.length + aArray.length );
var randArray = aArray[ rand - aArray.length ];
return randArray;
}

function nameoffunction(){
    //code in this function will be done after the window loads
    alert("random_array");
}

function alignarray(){
    var alignarray=['left','center','right'];
    var elem=document.getElementById('text-content');
    elem.style.textAlign=alignarray[Math.round(Math.random()*2)];
    nameoffunction();
}
</script>
</html>