简体   繁体   English

定位动态添加的元素

[英]Targeting Dynamically Added Elements

I'm trying to target multiple items on a page that are loaded dynamically via javascript. 我正在尝试定位通过javascript动态加载的页面上的多个项目。 The code I'm using is below and it works fine if the items are present in the DOM on load. 我正在使用的代码如下,如果加载时DOM中存在这些项目,则可以正常工作。

$(".target-item").each(function(i, element) {
   var innerURL =  $(this).html()
   $(element).html("<img src='"+ innerURL + "'>");
});

Is this possible to do? 这可能吗?

The code you posted will only ever work on the DOM elements currently on the page. 您发布的代码仅适用于当前页面上的DOM元素。 That's the nature of scripting, it runs once and then it's over -- so anything you add later to the page will be unaffected. 这就是脚本的本质,脚本运行一次,然后结束-因此,以后添加到页面的任何内容均不会受到影响。

You mentioned that something in your WordPress theme/plugin is responsible for adding those items to the DOM. 您提到WordPress主题/插件中的某些内容负责将这些项目添加到DOM。 The easiest way would be to look into that js and see if there's a way to integrate with it. 最简单的方法是查看该js,看看是否有与其集成的方法。 Does it trigger an event after it does this (you could listen for the event and then do your thing)? 执行此操作后是否会触发事件(您可以监听事件,然后执行您的操作)? Does it let you specify a callback function to be run after it does this (you could give it your img src logic as a function)? 它是否允许您指定执行此操作后要运行的回调函数(可以将img src逻辑作为函数提供给它)? If there's no way to integrate with it ... well, that's the downside of using third-party code. 如果没有办法与之集成……那么,这就是使用第三方代码的缺点。

However, I think you should be able to call this logic when the elements have been added to the page, regardless of how it happens. 但是,我认为在将元素添加到页面后,无论它如何发生,您都应该能够调用此逻辑。 Every DOM element triggers a 'load' event when it's loaded into the page, so you can listen for that. 每个DOM元素在加载到页面中时都会触发一个“加载”事件,因此您可以侦听该事件。 The elements don't exist yet, though, so you can't bind an event listener to them -- you have to use event delegation , and bind an event to the target element's parent. 但是,元素尚不存在,因此您不能将事件侦听器绑定到它们-您必须使用事件委托 ,并将事件绑定到目标元素的父对象。 Here's how it might look: 这是它的外观:

var targetParent = jQuery('.some-div-that-contains-dynamic-elements');
targetParent.on('load', '.target-item', function() {
  var $this = jQuery(this);
  var innerURL =  $this.html();
  $this.html("<img src='"+ innerURL + "'>");
});

Here you're binding an event listener on the element that contains your target-items. 在这里,您将事件监听器绑定到包含目标项目的元素上。 When a new target-item is added to the DOM, it's load event fires, bubbles up to the parent, and triggers the event handler. 将新的目标项目添加到DOM时,将触发load事件,气泡上升到父项,并触发事件处理程序。

So you have a div or something and inside is your url that you replace with an img element right? 因此,您有一个div或其他内容,并且您的网址里面是被img元素替换的网址了吗? And the Problem is that you don't know wich one is already replaced? 问题是您不知道哪一个已经被替换?

If I'm right than you can use ".target-item :not(:has(img))". 如果我没错,则可以使用“ .target-item:not(:has(img))”。 With that selector you will get all elements that has the class .target-item but no img element as child. 使用该选择器,您将获得所有具有.target-item类但没有img元素作为子元素的所有元素。

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

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