【问题标题】:Changing picture in javascript form on a click单击以更改 javascript 表单中的图片
【发布时间】:2011-02-15 03:22:37
【问题描述】:

在此代码中,我收到以下错误:异常...“索引或大小为负数或大于允许的数量”代码:“1”nsresult:“0x80530001 (NS_ERROR_DOM_INDEX_SIZE_ERR)”。这是什么原因造成的?谢谢

    function makecard(){ 
    var bodypaint = document.getElementById('minaj'); 
    var recipient = document.getElementById("recipient").value 
    var radioboxes = document.forms["cardform"].phrase.length 
    var i = document.getElementById("color").selectedIndex; 
    var z = document.getElementById("city").selectedIndex; 
    var tchatche= document.getElementById("color").options[i].text; 
    var malouba= document.getElementById("city").options[z].value; 

    for(c=0; c<radioboxes; c++){ 
        if( document.forms["cardform"].phrase[c].checked){ 
            var phrasevalue=document.forms["cardform"].phrase[c].value; 
            break; 
        } 
    } 
    if(document.getElementById("color").options[i].text === "White"){ 
        bodypaint.style.backgroundColor ="White" 
    } 
    if(document.getElementById("color").options[i].text === "Red"){ 
        bodypaint.style.background ="Red" 
    } 
    if(document.getElementById("color").options[i].text === "Blue"){ 
        bodypaint.style.backgroundColor ="Blue" 
    } 
    var selectedcity = document.forms["cardform"].city.value 
    var paragraph = document.createElement("div"); 
    paragraph.setAttribute("id","card") 
    document.body.appendChild(paragraph) 
    var picture = document.createElement("img") 
    picture.setAttribute("src", "") 
    paragraph.appendChild(picture) 
    paragraph.appendChild(document.createTextNode(phrasevalue + " from " + selectedcity + recipient)) 

    if(malouba== "Paris"){ 
        document.getElementById("picture").src = "paris.jpg" 
    } 


    if(malouba== "Venice"){ 
        document.getElementById("picture").src = "venice.jpg" 
    } 


    if(malouba== "Rome"){ 
        document.getElementById("picture").src = "rome.jpg" 
    } 
} 
document.getElementById("makeacard").onclick = makecard; 

【问题讨论】:

    标签: javascript html exception forms


    【解决方案1】:

    很可能其中一个语句生成了一个由 0 元素组成的数组,然后当您稍后尝试使用它时,它就会崩溃:

        var radioboxes = document.forms["cardform"].phrase.length
    

    对此做一点console.log(radioboxes),看看它是否返回未定义或为空

    【讨论】:

    • 感谢您的回复。我试过你的建议,这很有帮助。但是现在在 IE 中,当我尝试更改背景颜色时,它只是闪烁而不是永久更改。
    • 您可能应该为此发布一个新问题:)
    • 好吧,我确实从选择框 selectedIndex 属性中得到 0。即使数组从 0 开始,它还会吓坏吗?
    • 一个数组通常从@0开始,所以IE问题很可能完全不同......然后又......它是IE
    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多