简体   繁体   English

在屏幕上所有匹配的元素上运行href.replace

[英]Run href.replace on all matching elements on screen

Right now, I'm being forced to run my method on mouseover, which essentially changes part of the URL of matching links to something else. 现在,我被迫在鼠标悬停时运行我的方法,这实际上将匹配链接的URL的一部分更改为其他内容。 If I just hit all the elements on the entire page, I get a huge performance hit and even crashes. 如果我仅点击整个页面上的所有元素,就会对性能产生巨大的影响,甚至崩溃。 If I use the mouseover action, it doesn't function properly with a 3rd-party Chrome extension that it's intended to work with, but at least is stable. 如果我使用mouseover动作,则该鼠标不能与它打算使用的第三方Chrome扩展程序一起正常工作,但至少是稳定的。

Here's where I'm at: 我在这里:

$('body').on('mouseover', 'a', function(e) {
  e.preventDefault();
  if(this.href.match(/example.com/g)){
      this.href = this.href.replace('example.com', 'newexample.org');
  }
});

I'd like to, instead of executing this .replace on mouseover, instead take all example.com links and convert them when they're on screen, including as you scroll down. 我想而不是在鼠标悬停时执行此.replace ,而是采用所有example.com链接并在屏幕上显示时进行转换,包括向下滚动时进行转换。 That way, it breaks up all the links into slightly more manageable chunks. 这样,它将所有链接分解为稍微更易于管理的块。 That's the only way I can think of doing it without hitting all the page links at once and taking the performance hit. 这是我可以想到的唯一方法,而无需立即点击所有页面链接并降低性能。

Is it possible? 可能吗?

Install the jQuery Viewport extension from http://www.appelsiini.net/projects/viewport http://www.appelsiini.net/projects/viewport安装jQuery Viewport扩展

Then you can use a scroll handler that does something like: 然后,您可以使用执行以下操作的滚动处理程序:

$('a:in-viewport[href*="example.com"]').each(function() {
    this.href = this.href.replace('example.com', 'newexample.org');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM