Javascript 未捕获类型错误:无法读取属性';innerHTML';未定义的

Javascript 未捕获类型错误:无法读取属性';innerHTML';未定义的,javascript,prototypejs,Javascript,Prototypejs,我一直在思考这个问题,我通常是一个jQuery的小伙子,但是我有一个使用prototype的Magento扩展,我对如何修复这个错误有点迷茫: 我得到了错误:Uncaught TypeError:无法读取未定义的属性“innerHTML”我猜是因为我需要检查相关页面中是否存在某些HTML(javascript文件出现在每个页面上,因此可能每次都没有相关的HTML) 最初我得到了一个spConfig undefined,这就是为什么我在if((typeof spConfig=='undefined

我一直在思考这个问题,我通常是一个jQuery的小伙子,但是我有一个使用prototype的Magento扩展,我对如何修复这个错误有点迷茫:

我得到了错误:
Uncaught TypeError:无法读取未定义的属性“innerHTML”
我猜是因为我需要检查相关页面中是否存在某些HTML(javascript文件出现在每个页面上,因此可能每次都没有相关的HTML)

最初我得到了一个
spConfig undefined
,这就是为什么我在
if((typeof spConfig=='undefined')| |!spConfig){
行中添加了
if((typeof spConfig=='undefined')

原代码为:

document.observe("dom:loaded", function() {
if(spConfig.config.dynamics == 1 && spConfig.config.showbottom == 1){
    $$('.product-options-bottom')[0].insert({top: new Element('p', {id:'bottom-avail', class:'availability'}).update($$('p.availability')[0].innerHTML)});
    if(spConfig.config.showship == 1){
        $('bottom-avail').insert({after: new Element('p', {id:'bottom-shipsin', class:'shipsin'}).update($$('p.shipsin')[0].innerHTML)}); }
}
});
我试着换成

document.observe("dom:loaded", function() {
if ((typeof spConfig == 'undefined') || !spConfig) {

} else {
    if(spConfig.config.dynamics == 1 && spConfig.config.showbottom == 1){
        if($$('p.availability') != 'undefined' ){
            $$('.product-options-bottom')[0].insert({top: new Element('p', {id:'bottom-avail', class:'availability'}).update($$('p.availability')[0].innerHTML)});
        }
        if(spConfig.config.showship == 1){
            if($$('p.shipsin') != 'undefined'){
                $('bottom-avail').insert({after: new Element('p', {id:'bottom-shipsin', class:'shipsin'}).update($$('p.shipsin')[0].innerHTML)}); 
            }
        }
}
}
});
但是我仍然得到错误
Uncaught TypeError:无法读取指向行
$('.product options bottom')[0]的未定义的
的属性'innerHTML'。插入({top:new Element('p',{id:'bottom-available',class:'availability'))。更新($$('p.availability')[0].innerHTML);

,因为
$()
方法返回与CSS选择器匹配的元素列表(或数组)。您无法检查其
是否未定义,但可以检查列表中返回的元素数

所以试试这个

if($$('p.availability').length != 0)
{
    $$('.product-options-bottom')[0].insert(
        {
            top: new Element('p', {id:'bottom-avail', 'class':'availability'}).update(
                $$('p.availability')[0].innerHTML
            )});
}

另外,
new Element()
中的
class
属性应该是一个字符串,因为class在某些浏览器中是一个关键字,也将成为ECMA 6中核心Javascript语言的一部分

感谢class提示,它解决了我在进行长度检查时遇到的下一个错误!