我在 jQuery 中有这段代码,我想用原型库重新实现它。

// make external links open in popups
// this will apply a window.open() behaviour to all anchor links
// the not() functions filter iteratively filter out http://www.foo.com 
// and http://foo.com so they don't trigger off the pop-ups

jQuery("a[href='http://']").
      not("a[href^='http://www.foo.com']").
      not("a[href^='http://foo.com']").
      addClass('external');

jQuery("a.external").
      not('a[rel="lightbox"]').click( function() {
      window.open( jQuery(this).attr('href') );
      return false;
});

如何使用 jQuery 中列出的 not() 运算符的等效项来迭代过滤元素集合?

有帮助吗?

解决方案

可以使用拒绝方法来完成过滤,如下所示:

$$('a').reject(function(element) { return element.getAttribute("href").match(/http:\/\/(www.|)foo.com/); }).invoke("addClassName", "external");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top