Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript window.onload在我将其设置为两个不同的函数时仅运行一个函数_Javascript_Html_Javascript Events - Fatal编程技术网

Javascript window.onload在我将其设置为两个不同的函数时仅运行一个函数

Javascript window.onload在我将其设置为两个不同的函数时仅运行一个函数,javascript,html,javascript-events,Javascript,Html,Javascript Events,我对我编写的这两个脚本有一个问题,看起来好像底部的脚本都是函数,所以如果我将它们切换到底部的脚本,那么下面是帮助代码 <script type="text/javascript"> window.onload = function() { //Badge var eSelect = document.getElementById('leftbadge'); var yellowplate = document.getElementById('numberplateye

我对我编写的这两个脚本有一个问题,看起来好像底部的脚本都是函数,所以如果我将它们切换到底部的脚本,那么下面是帮助代码

<script type="text/javascript">
window.onload = function() {
//Badge
var eSelect = document.getElementById('leftbadge');
        var yellowplate = document.getElementById('numberplateyellow');
        var whiteplate = document.getElementById('numberplatewhite');
        eSelect.onchange = function() {
            if(eSelect.selectedIndex === 0) {
                yellowplate.style.backgroundImage = 'url("builder/yellow_bg.png")';
                whiteplate.style.backgroundImage = 'url("builder/white_bg.png")';
                yellowplate.style.textAlign = 'center';
                yellowplate.style.paddingRight = '0';
                whiteplate.style.textAlign = 'center';
                whiteplate.style.paddingRight = '0';
            }else if(eSelect.selectedIndex === 1) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_ENG.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_ENG.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }else if(eSelect.selectedIndex === 2) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_GB.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_GB.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }else if(eSelect.selectedIndex === 3) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_GB2.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_GB2.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }else if(eSelect.selectedIndex === 4) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_SCO.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_SCO.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }else if(eSelect.selectedIndex === 5) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_CYMRU.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_CYMRU.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }
        }
}
</script>
<script type="text/javascript">
//Font
        window.onload = function() {
        var eSelect = document.getElementById('font');
        var yellowplate = document.getElementById('numberplateyellow');
        var whiteplate = document.getElementById('numberplatewhite');
        eSelect.onchange = function() {
            if(eSelect.selectedIndex === 0) {
                yellowplate.style.fontFamily = 'JepsonCarRegular';
                whiteplate.style.fontFamily = 'JepsonCarRegular';
            } else {
                yellowplate.style.fontFamily = 'twotoneRegular';
                whiteplate.style.fontFamily = 'twotoneRegular';
            }
        }
    }
</script>

window.onload=函数(){
//徽章
var eSelect=document.getElementById('leftbadge');
var yellowplate=document.getElementById('numberplateyellow');
var whiteplate=document.getElementById('numberplatewhite');
eSelect.onchange=函数(){
如果(eSelect.selectedIndex==0){
yellowplate.style.backgroundImage='url(“builder/yellow_bg.png”);
whiteplate.style.backgroundImage='url(“builder/white_bg.png”);
yellowplate.style.textAlign='中心';
yellowplate.style.paddingRight='0';
whiteplate.style.textAlign='center';
whiteplate.style.paddingRight='0';
}else if(eSelect.selectedIndex==1){
yellowplate.style.backgroundImage='url(builder/yellow\u bg\u ENG.png)';
whiteplate.style.backgroundImage='url(builder/white\u bg\u ENG.png)';
//向右对齐,然后添加填充
yellowplate.style.textAlign='右';
yellowplate.style.paddingRight='10px';
whiteplate.style.textAlign='右';
whiteplate.style.paddingRight='10px';
}else if(eSelect.selectedIndex==2){
yellowplate.style.backgroundImage='url(builder/yellow\u bg\u GB.png)';
whiteplate.style.backgroundImage='url(builder/white\u bg\u GB.png)';
//向右对齐,然后添加填充
yellowplate.style.textAlign='右';
yellowplate.style.paddingRight='10px';
whiteplate.style.textAlign='右';
whiteplate.style.paddingRight='10px';
}else if(eSelect.selectedIndex==3){
yellowplate.style.backgroundImage='url(builder/yellow\u bg\u GB2.png)';
whiteplate.style.backgroundImage='url(builder/white\u bg\u GB2.png)';
//向右对齐,然后添加填充
yellowplate.style.textAlign='右';
yellowplate.style.paddingRight='10px';
whiteplate.style.textAlign='右';
whiteplate.style.paddingRight='10px';
}else if(eSelect.selectedIndex==4){
yellowplate.style.backgroundImage='url(builder/yellow\u bg\u SCO.png)';
whiteplate.style.backgroundImage='url(builder/white\u bg\u SCO.png)';
//向右对齐,然后添加填充
yellowplate.style.textAlign='右';
yellowplate.style.paddingRight='10px';
whiteplate.style.textAlign='右';
whiteplate.style.paddingRight='10px';
}else if(eSelect.selectedIndex==5){
yellowplate.style.backgroundImage='url(builder/yellow\u bg\u CYMRU.png)';
whiteplate.style.backgroundImage='url(builder/white\u bg\u CYMRU.png)';
//向右对齐,然后添加填充
yellowplate.style.textAlign='右';
yellowplate.style.paddingRight='10px';
whiteplate.style.textAlign='右';
whiteplate.style.paddingRight='10px';
}
}
}
//字体
window.onload=函数(){
var eSelect=document.getElementById('font');
var yellowplate=document.getElementById('numberplateyellow');
var whiteplate=document.getElementById('numberplatewhite');
eSelect.onchange=函数(){
如果(eSelect.selectedIndex==0){
yellowplate.style.fontFamily='JepsonCarRegular';
whiteplate.style.fontFamily='JepsonCarRegular';
}否则{
yellowplate.style.fontFamily='twotoneRegular';
whiteplate.style.fontFamily='twotoneRegular';
}
}
}

因此,在本例中,字体会改变,但如果我尝试在那里下拉,徽章不会改变。您将为window.onload分配两个操作。底部的函数将始终执行,因为它是最后一个分配给的函数(因此,它覆盖了以前的函数分配)

您应该考虑将2个onload操作合并为1,有点像这样:

<script type="text/javascript">
function handleBadges()
{
    //Badge
    var eSelect = document.getElementById('leftbadge');
    var yellowplate = document.getElementById('numberplateyellow');
    var whiteplate = document.getElementById('numberplatewhite');
    eSelect.onchange = function() 
    {
        if(eSelect.selectedIndex === 0) {
            yellowplate.style.backgroundImage = 'url("builder/yellow_bg.png")';
            whiteplate.style.backgroundImage = 'url("builder/white_bg.png")';
            yellowplate.style.textAlign = 'center';
            yellowplate.style.paddingRight = '0';
            whiteplate.style.textAlign = 'center';
            whiteplate.style.paddingRight = '0';
        }else if(eSelect.selectedIndex === 1) {
            yellowplate.style.backgroundImage = 'url(builder/yellow_bg_ENG.png)';
            whiteplate.style.backgroundImage = 'url(builder/white_bg_ENG.png)';
            //Align To Right and then add padding
            yellowplate.style.textAlign = 'right';
            yellowplate.style.paddingRight = '10px';
            whiteplate.style.textAlign = 'right';
            whiteplate.style.paddingRight = '10px';
        }else if(eSelect.selectedIndex === 2) {
            yellowplate.style.backgroundImage = 'url(builder/yellow_bg_GB.png)';
            whiteplate.style.backgroundImage = 'url(builder/white_bg_GB.png)';
            //Align To Right and then add padding
            yellowplate.style.textAlign = 'right';
            yellowplate.style.paddingRight = '10px';
            whiteplate.style.textAlign = 'right';
            whiteplate.style.paddingRight = '10px';
        }else if(eSelect.selectedIndex === 3) {
            yellowplate.style.backgroundImage = 'url(builder/yellow_bg_GB2.png)';
            whiteplate.style.backgroundImage = 'url(builder/white_bg_GB2.png)';
            //Align To Right and then add padding
            yellowplate.style.textAlign = 'right';
            yellowplate.style.paddingRight = '10px';
            whiteplate.style.textAlign = 'right';
            whiteplate.style.paddingRight = '10px';
        }else if(eSelect.selectedIndex === 4) {
            yellowplate.style.backgroundImage = 'url(builder/yellow_bg_SCO.png)';
            whiteplate.style.backgroundImage = 'url(builder/white_bg_SCO.png)';
            //Align To Right and then add padding
            yellowplate.style.textAlign = 'right';
            yellowplate.style.paddingRight = '10px';
            whiteplate.style.textAlign = 'right';
            whiteplate.style.paddingRight = '10px';
        }else if(eSelect.selectedIndex === 5) {
            yellowplate.style.backgroundImage = 'url(builder/yellow_bg_CYMRU.png)';
            whiteplate.style.backgroundImage = 'url(builder/white_bg_CYMRU.png)';
            //Align To Right and then add padding
            yellowplate.style.textAlign = 'right';
            yellowplate.style.paddingRight = '10px';
            whiteplate.style.textAlign = 'right';
            whiteplate.style.paddingRight = '10px';
        }
    }
}    
function handleFonts()
{
    var eSelect = document.getElementById('font');
    var yellowplate = document.getElementById('numberplateyellow');
    var whiteplate = document.getElementById('numberplatewhite');
    eSelect.onchange = function() {
        if(eSelect.selectedIndex === 0) {
            yellowplate.style.fontFamily = 'JepsonCarRegular';
            whiteplate.style.fontFamily = 'JepsonCarRegular';
        } else {
            yellowplate.style.fontFamily = 'twotoneRegular';
            whiteplate.style.fontFamily = 'twotoneRegular';
        }
    }
}
window.onload = function() 
{
    handleBadges()
    handleFonts()
}
</script>

功能把手板()
{
//徽章
var eSelect=document.getElementById('leftbadge');
var yellowplate=document.getElementById('numberplateyellow');
var whiteplate=document.getElementById('numberplatewhite');
eSelect.onchange=函数()
{
如果(eSelect.selectedIndex==0){
yellowplate.style.backgroundImage='url(“builder/yellow_bg.png”);
whiteplate.style.backgroundImage='url(“builder/white_bg.png”);
yellowplate.style.textAlign='中心';
yellowplate.style.paddingRight='0';
whiteplate.style.textAlign='center';
whiteplate.style.paddingRight='0';
}else if(eSelect.selectedIndex==1){
yellowplate.style.backgroundImage='url(builder/yellow\u bg\u ENG.png)';
whiteplate.style.backgroundImage='url(builder/white\u bg\u ENG.png)';
//向右对齐,然后添加填充
yellowplate.style.textAlign='右';
yellowplate.style.paddingRight='10px';
whiteplate.style.textAlign='右';
whiteplate.style.paddingRight='10px';
}else if(eSelect.selectedIndex==2){
yellowplate.style.backgroundImage='url(builder/yellow\u bg\u GB.png)';
whiteplate.style.backgroundImage='url(builder/white\u bg\u GB.png)';
//向右对齐,然后添加填充
yellowplate.style.textAlign='右';
<script type="text/javascript">
window.addEventListener("load", function() {
//Badge
var eSelect = document.getElementById('leftbadge');
        var yellowplate = document.getElementById('numberplateyellow');
        var whiteplate = document.getElementById('numberplatewhite');
        eSelect.onchange = function() {
            if(eSelect.selectedIndex === 0) {
                yellowplate.style.backgroundImage = 'url("builder/yellow_bg.png")';
                whiteplate.style.backgroundImage = 'url("builder/white_bg.png")';
                yellowplate.style.textAlign = 'center';
                yellowplate.style.paddingRight = '0';
                whiteplate.style.textAlign = 'center';
                whiteplate.style.paddingRight = '0';
            }else if(eSelect.selectedIndex === 1) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_ENG.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_ENG.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }else if(eSelect.selectedIndex === 2) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_GB.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_GB.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }else if(eSelect.selectedIndex === 3) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_GB2.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_GB2.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }else if(eSelect.selectedIndex === 4) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_SCO.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_SCO.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }else if(eSelect.selectedIndex === 5) {
                yellowplate.style.backgroundImage = 'url(builder/yellow_bg_CYMRU.png)';
                whiteplate.style.backgroundImage = 'url(builder/white_bg_CYMRU.png)';
                //Align To Right and then add padding
                yellowplate.style.textAlign = 'right';
                yellowplate.style.paddingRight = '10px';
                whiteplate.style.textAlign = 'right';
                whiteplate.style.paddingRight = '10px';
            }
        }
});
</script>
<script type="text/javascript">
//Font
    window.addEventListener("load",function() {
        var eSelect = document.getElementById('font');
        var yellowplate = document.getElementById('numberplateyellow');
        var whiteplate = document.getElementById('numberplatewhite');
        eSelect.onchange = function() {
            if(eSelect.selectedIndex === 0) {
                yellowplate.style.fontFamily = 'JepsonCarRegular';
                whiteplate.style.fontFamily = 'JepsonCarRegular';
            } else {
                yellowplate.style.fontFamily = 'twotoneRegular';
                whiteplate.style.fontFamily = 'twotoneRegular';
            }
        }
    });
</script>