开发者

Prototype find element and only perform if found

开发者 https://www.devze.com 2022-12-21 07:11 出处:网络
Struggling a little here with prototype = I\'ve created this function 开发者_运维百科 Event.observe(window, \'load\', function() {

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

精彩评论

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

关注公众号