简体   繁体   English

jQuery在动态生成内容时获取被点击元素的属性

[英]jQuery get attribute of clicked element when content is dynamically generated

I am working on a site where a lot of content is generated dynamically using AngularJS.我正在使用 AngularJS 动态生成大量内容的站点上工作。 I need to get the attribute of an element that is dynamically generated using jQuery, but I am having trouble doing so.我需要获取使用 jQuery 动态生成的元素的属性,但我在这样做时遇到了问题。 I have already figured out that I need to use the .on method to click rather than .click .我已经想通了,我需要使用的.on的方法来点击,而不是.click My issue is finding the equivalent of $(this) within the .on method.我的问题是在.on方法中找到等效的$(this)

Here is my code:这是我的代码:

$(document.body).on('click', 'a.card-topic-link' ,function(e) {
    e.preventDefault(); // Prevent default action - this works

    console.log('The click works!'); // This fires and works just fine

    var cardTopicLink = $(this).attr('data-topic-link'); // This is where the problem lies

    console.log(cardTopicLink); // This is undefined
}); // End on click

As I stated before, this does not work as the .click does not work for dynamic content:正如我之前所说,这不起作用,因为.click不适用于动态内容:

$('a.card-topic-link').click(function(e) {
    e.preventDefault(); // Prevent default action

    var cardTopicLink = $(this).attr('data-topic-link');

    console.log(cardTopicLink);
});

I feel like there is a simple solution to this problem that I am having trouble finding.我觉得这个问题有一个简单的解决方案,但我很难找到。 The key to this issue is that this is dealing with dynamic content.这个问题的关键是这是在处理动态内容。

Let me know if you have any ideas.如果您有任何想法,请告诉我。

use e.currentTarget, instead of var cardTopicLink = $(this).attr('data-topic-link');使用 e.currentTarget,而不是var cardTopicLink = $(this).attr('data-topic-link'); , try var cardTopicLink = angular.element(e.currentTarget).attr('data-topic-link'); , 试试var cardTopicLink = angular.element(e.currentTarget).attr('data-topic-link');

$(document).ready(function () {
  $(document.body).on('click', 'a.card-topic-link' ,function(e) {
    e.preventDefault(); // Prevent default action - this works
    console.log('The click works!'); // This fires and works just fine

    var cardTopicLink = $(e.currentTarget).attr('data-topic-link'); //   This is where the problem lies

    console.log(cardTopicLink);
  }); // End on click
});

plunker with similar code, firing on async loaded content - http://plnkr.co/edit/02rqCrbBVwXiIYff8ktC?p=preview具有类似代码的 plunker,在异步加载的内容上触发 - http://plnkr.co/edit/02rqCrbBVwXiIYff8ktC?p=preview

It turns out that AngularJS was stripping out the attribute data-topic-link as it was parsed.事实证明,AngularJS 在解析时剥离了属性data-topic-link This was happening because the HTML that contained the data-topic-link was passed as a part of the response that Angular was printing in an ng-repeat .发生这种情况是因为包含data-topic-link的 HTML 作为 Angular 在ng-repeat打印的响应的一部分被传递。

I updated the code so that the HTML with this attribute did not need to be served by AngularJS.我更新了代码,以便 AngularJS 不需要提供具有此属性的 HTML。

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

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