2014-11-01 12 views
1

我想檢查標籤是否有ID selected。 我的html:按id選擇標籤內的元素,並檢查它的類 - 原型

<div id="30-super-attribute-138" class="colorswatch-lines"> 
     <a class="lnk-colorswatch selected" id="30-lnk-colorswatch-138-34" href="javascript:selectColorSwatch(138, 34, 30, true)"></a> 
    </div> 

和我的原型腳本:

if ($('30-super-attribute-138 a').hasClassName('selected')) { 
     var myStyle = '#333'; 
     $('main-picture').setStyle ({ 
      backgroundColor: myStyle 
     }); 
    } 

的setStyle工作正常,但我有麻煩來檢查,如果標籤a有一流的選擇 - 它發生了,選擇後顏色。 在控制檯我看到錯誤:Uncaught TypeError: Cannot read property 'hasClassName' of null

30-super-attribute-138

如何從內部DIV正確選擇標籤的ID爲謝謝彼得

回答

1

...嗯這很簡單,拿到第一個子元素與原型:

if ($('30-super-attribute-138').down('a').hasClassName('selected')) { 
    var myStyle = '#333'; 
    $('main-picture').setStyle ({ 
     backgroundColor: myStyle 
    }); 
} 

好吧,現在看起來很簡單對於這種問題感到抱歉。

+0

...我將'.down('a')。hasClassName('selected')'修改爲'.down('a.selected')' – PeterB 2014-11-01 16:16:26

0

對於在PrototypeJS做的另一種方式,你可能會發現有用/有趣,這可以在一個單一的DOM調用工作着想:

if($$('#30-super-attribute-138 a.selected')[0]) { 
    var myStyle = '#333'; 
    $('main-picture').setStyle ({ 
     backgroundColor: myStyle 
    }); 
} 

編碼快樂!