您现在的位置:龙卷风首页 ›› 网络编程 ›› 阅读文章

jquery操作元素具备唯一性

jquery 元素

jquery选择页面中的元素,这些元素是真的被“选择出来”了,如果你把这些元素用于其它地方,原来的元素将会消失!

这是我在做潮汕风情网时发现的。我的设想是通过jquery把文章内容中的某些链接提取出来,统一放到文章内容的下面,而文章内容要保持不变。起初的代码是这样写的:

代码如下
  1. $("#peopleLinkExtend").append("<ul></ul>");
  2. $(".people_link").each(function(){
  3.     $("#peopleLinkExtend ul").append("<li></li>");
  4.     $("#peopleLinkExtend ul li:last").append(this);
  5.     $("#peopleLinkExtend ul li:last").append("的相关资料");
  6. });

结果原来文章中的链接消失了。后来想起jQuery有克隆元素的功能,于是改成这样:

代码如下
  1. $("#peopleLinkExtend").append("<ul></ul>");
  2. $(".people_link").clone().each(function(){
  3.     $("#peopleLinkExtend ul").append("<li></li>");
  4.     $("#peopleLinkExtend ul li:last").append(this);
  5.     $("#peopleLinkExtend ul li:last").append("的相关资料");
  6. });

结果就正常了。参考实例页面:http://www.csfqw.com/html/6/2008112511522663.html

作者 不见不散 本文仅代表作者观点,与龙卷风资讯网立场无关。

我来说两句

内容/Content