开发者

Prototype find element and only perform if found

Struggling a little here with prototype = I've created this function

开发者_运维百科
Event.observe(window, 'load', function() {

if ($$('#test li:first')!=null) {
   $('test').down('li').down('a').update('Test');
}
});

Basically, it works on pages where #test li:first is found but on pages it isn't it returns

$("test").down("li") is undefined

I just wanted it to basically work only if it found the element otherwise not work ?

Edit: Solved

Event.observe(window, 'load', function() {

try { 

if ($$('#test li:first')!=null) {
   $('test').down('li').down('a').update('Test');
} } 

catch(ex) {
}

});


That's not really solving it; you are just catching an error that is easily fixed.

Try this on for size:

$$('#test li:first-child').invoke('update', 'Test');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜