简体   繁体   English

在动态添加的元素上添加目标

[英]Add target on elements added dynamically

I'm a novice in jQuery and I got stuck whit a problem for a while now. 我是jQuery的新手,现在有一段时间困扰我。 I am trying to discover all the links to an external page and add a target="_blank" attribute to them. 我试图发现到外部页面的所有链接,并向它们添加target =“ _ blank”属性。 So far I have created the following script: 到目前为止,我已经创建了以下脚本:

$(document).on('load',function() {
$('a').each(function () {
    if (location.hostname !== this.hostname) {
        $(this).attr('target', '_blank');
    }
});
});

This works fine on the elements that are loaded on the page in the first instance, but when new elements are added dynamically it doesn't fire again to add the target attribute. 这在第一个实例中在页面上加载的元素上效果很好,但是当动态添加新元素时,它不会再次触发以添加target属性。 I have tried to change the "ready" function to "on('load')" but to no success. 我试图将“就绪”功能更改为“ on('load')”,但没有成功。

Any help is highly appreciated and I am grateful for the people who take a second to answer silly questions like this. 我们非常感谢您的帮助,对于能抽出时间回答此类愚蠢问题的人们,我深表感谢。

Edit: I created and infinite scroll script that loads more elements through an AJAX call. 编辑:我创建了一个无限滚动脚本,该脚本通过AJAX调用加载了更多元素。 This elements contain "a" tags and I would like the script to run again for them. 该元素包含“ a”标签,我希望脚本再次为其运行。

You have to trigger that function whenever you add new elements. 每当添加新元素时,都必须触发该函数。 Because you is the best person to know when new links are being added, so you have to set up the scripts in that way. 因为您是知道何时添加新链接的最佳人,所以您必须以这种方式设置脚本。

function setTargetBlank () {
  $('a').each(function () {
    if (location.hostname !== this.hostname) {
      $(this).attr('target', '_blank');
    }
  });
}

// On load
$(function () {
  setTargetBlank();
  loadSomeContent(function () {
    // And after it is loaded, trigger again the function
    setTargetBlank();
  });
});

If you cannot control these, you can simply set up a timer which will always trigger that function every few seconds: 如果您无法控制这些,则可以简单地设置一个计时器,该计时器将始终每隔几秒钟触发一次该功能:

 // This is not really good for performance, but it will work
 // It will trigger the function every 3 seconds
 setInterval(setTargetBlank, 3 * 1000);

I would wrap your link-changing code in a function which you can call a) onload and b) when your content changes. 我会将您的链接更改代码包装在一个函数中,当您的内容更改时,可以调用a)onload和b)。

$(document).ready(function() {
  linkChanger();
});

function linkChanger() {
  $('a').each(function () {
      if (location.hostname !== this.hostname) {
          $(this).attr('target', '_blank');
      }
  });
}

then if you add content/links later, you can call linkChanger() to check/convert your page again: 然后,如果以后添加内容/链接,则可以调用linkChanger()再次检查/转换页面:

$('#something').click(function () {
  // code that gets more content, if async provide a callback that calls linkChanger()
  linkChanger();
});

It is possible that you are targeting the A links before the new elements have been created, Try running the code in a setTimeout function. 在创建新元素之前,可能已将A链接定位为目标。尝试在setTimeout函数中运行代码。 for example 例如

$(document).on('load',function() {

setTimeout(function(){ $('a').each(function () { if (location.hostname !== this.hostname) { $(this).attr('target', '_blank'); setTimeout(function(){ $('a').each(function () { if (location.hostname !== this.hostname) { $(this).attr('target', '_blank');
} }); } }); }, 5000);

});

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

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