简体   繁体   English

jQuery:选择新添加的DOM节点的最佳方法是什么?

[英]Jquery: What is the best way to select the newly added DOM node?

I have an external template file with a structure that is dynamically added to a table when a button is clicked. 我有一个外部模板文件,该文件的结构是单击按钮时动态添加到表中的。 When the new table line is added this way, I need to call a callback on this newly added piece of code. 当以这种方式添加新表行时,我需要在此新添加的代码段上调用回调。

I would like to know, what is the best way to select this new code. 我想知道,选择此新代码的最佳方法是什么。 It has an ID, so it won't be a problem to write some other selector, but I was wondering if there is an easier and fast way how to select it. 它有一个ID,因此编写其他选择器不会有问题,但是我想知道是否有更简便的方法来选择它。

Here is the piece of code: 这是一段代码:

$rightSpot.append(app.renderTemplate('temp/product', tplData ));

EDIT: This works for me, but maybe there is a better way: 编辑:这对我有效,但也许有更好的方法:

$newProduct = $rightSpot.find('tr').last().find('textarea');

I would consider using react.js. 我会考虑使用react.js。 The advantage of using react is that it works with a virtual DOM so you will not be reliant of the physical DOM to render before your callback is available. 使用react的好处是它可以与虚拟DOM一起使用,因此在回调可用之前,您将不会依赖要呈现的物理DOM。

http://facebook.github.io/react/ http://facebook.github.io/react/

I don't know if it is the best way but you could do something like this - 我不知道这是否是最好的方法,但是您可以执行以下操作-

 $("#giveitafriend").click(function() { $("#ab").append($("<span>") .text("I'm here too!") .css("color", "red") ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="ab"> <span>On My Own</span> </div> <button id="giveitafriend">Give it a Friend</button> 

Assuming your renderTemplate function returns HTML element, may be you can access the new element as follows 假设您的renderTemplate函数返回HTML元素,也许您可​​以按如下方式访问新元素

var newElement = app.renderTemplate('temp/product', tplData ); 
$rightSpot.append(newElement);
console.log($(newElement).attr('id'));

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

相关问题 jQuery function 未绑定到新添加的 dom 元素 - jQuery function not binding to newly added dom elements 使用jQuery将HTML注入DOM的最佳方法是什么? - What is the best way to inject HTML into the DOM with jQuery? 使用jQuery和Node在DOM上填充数据的最佳方法 - Best way to populate data on DOM using jQuery and Node 查找元素DOM(阴影或光)的根节点的最佳方法是什么 - What is the best way to find root node of elements DOM (shadow or light) jQuery-在新添加的DOM对象上动态使用&#39;.filter&#39; - jQuery - dynamically using '.filter' on newly added DOM objects 在jQuery中选择DOM元素-动态添加 - Select DOM elements in jQuery - Dynamically Added 什么是计算html dom节点中字符数的最佳方法(在使用javascript rangy库的文档节点中) - What is the best way to calculate number of characters in html dom node (in document node using javascript rangy library) jQuery treeview插件折叠/扩展到新添加的节点 - jQuery treeview plugin collapse/expand to the newly added node 使用 jQuery 向 JavaScript 对象中的选择添加选项的最佳方法是什么? - What is the best way to add options to a select from a JavaScript object with jQuery? 使用 jQuery 添加 DOM 元素的最佳方式 - Best way to add DOM elements with jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM