Javascript 原型中的漂亮代码,如何省略';不是一个函数';空结果选择器上的错误

Javascript 原型中的漂亮代码,如何省略';不是一个函数';空结果选择器上的错误,javascript,prototype,Javascript,Prototype,当我在jQuery中执行此操作时,li.notsuchelem不存在 $('li.notsuchelem').closest('div') 它很好用 但当我在原型中做同样的事情时,它会给我一个错误 $$('li.notsuchelem').up('div') >TypeError: $$('li.notsuchelem').up is not a function 如何从以下内容生成漂亮的代码: if ($$('li.notsuchelem').length) $$('li.nots

当我在jQuery中执行此操作时,
li.notsuchelem
不存在

$('li.notsuchelem').closest('div')
它很好用

但当我在原型中做同样的事情时,它会给我一个错误

$$('li.notsuchelem').up('div')

>TypeError: $$('li.notsuchelem').up is not a function
如何从以下内容生成漂亮的代码:

if ($$('li.notsuchelem').length) $$('li.notsuchelem').up('div');

>>>>

var tmp;
if ((tmp = $$('li.notsuchelem')).length) tmp.up('div');

>>>>

$$('li.notsuchelem').each(function(el){el.up('div')});
在我看来,一切都和jQuery相反。我能做点什么吗,还是我必须处理


嗯,我想我会接受答案,但要么我编码错误,要么我就是不喜欢原型

jQuery:

$('.ss_new:not(.force_visible)', wrapper).closest('tr').hide();
原型:

wrapper.select('.ss_new:not(.force_visible)').invoke('up','tr').invoke('hide');

您可以将返回的数组强制为数字,而不是长度。此外,还可以使用逻辑AND(
&&
)运算符的副作用

+$$('li.notsuchelem') && $$('li.notsuchelem').up('div');

您可以将返回的数组强制为数字,而不是长度。此外,还可以使用逻辑AND(
&&
)运算符的副作用

+$$('li.notsuchelem') && $$('li.notsuchelem').up('div');

我认为prototypejs是这样的

$$(".test").invoke("up", "div");

我认为prototypejs是这样的

$$(".test").invoke("up", "div");

嗯,它几乎与
if($$('li.notsuchelem').length)$$('li.notsuchelem').up('div')。再短一点。但是我仍然调用了搜索两次,并使用了某种
if
,它几乎与
if($$('li.notsuchelem').length)$$('li.notsuchelem').up('div')。再短一点。但我仍然调用了搜索两次,并使用了某种
,如果它看起来像我想要的,需要检查文档中的
调用
。类似于
每一个
,但更短、更好看这似乎是我想要的,需要检查文档中的
调用
。类似于每个
,但更短、更好看