gpt4 book ai didi

jquery - 如何获取对刚刚通过操作方法添加的内容的 jQuery 选择器的引用?

转载 作者:行者123 更新时间:2023-12-03 22:20:37 25 4
gpt4 key购买 nike

如果使用以下内容将内容添加到 DOM,

$("ul").append("<li>test</li>");

如何获得刚刚添加的内容的引用而无需选择新添加的内容?

指定append()方法的返回值是jQuery对象。

var newContent=$("ul").append("<li>test</li>");

一个人可以做到

var newContent=$("ul li:last");

但是有没有办法更直接地获取它呢?

谢谢

最佳答案

使用.appendTo() :

Insert every element in the set of matched elements to the end of the target...

The .append() and .appendTo() methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With .append(), the selector expression preceding the method is the container into which the content is inserted. With .appendTo(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container...

关于jquery - 如何获取对刚刚通过操作方法添加的内容的 jQuery 选择器的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2179318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com