如何创建一个空的,非空的jQuery对象准备好追加? [英] How to create an empty, non-null jQuery object ready for appending?

查看:639
本文介绍了如何创建一个空的,非空的jQuery对象准备好追加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在循环中将一些html添加到一个空的非null jQuery对象中,但它不起作用,除非我在创建过程中创建一个对象并添加一个html标签。如何创建一个空的jQuery对象,并可以稍后添加html?

I want to append some html to an empty non null jQuery object only in the loop but it's not working unless I create an object and add an html tag during creation. How can I create an empty jQuery object and be able to add html to it later on?

//var $new = $().add("");  //can't use this object in the loop
//var $new = $([]);        //can't use this object in the loop
//var $new = $();        //can't use this object in the loop
//var $new = $("#nonexistingelement"); //can't use this object in the loop
var $new = $().add("<tr>");  //works but I don't want to add any html at this point. 

$.each(.....)
  {
    $new.append("<sometag>");

});

alert($new.html());  //should display some html

添加:
http://jsfiddle.net/vfPNT /

推荐答案

我找不到另一种方式,所以我开始使用< tr> 。删除< tr> 同时保持内容不能正常工作,这就是为什么我发布了这个问题。所以,相反,我通过选择< tr> 中的所有< sometag>来创建一个新的jQuery选择,并抛出$ new。 >

I couldn't find another way so I started with the <tr>. Removing the <tr> while keeping the contents didn't work properly and that's why I posted the question. So, instead, I created a new jQuery selection by selecting all the <sometag>'s which were inside the <tr> and threw away $new.

var $new = $().add("<tr>");  

$.each(.....)
  {
    $new.append("<sometag>");

});
$new.append("</tr>");
$newObject = $('sometagt', $new)
..... do more work using $newObject

这篇关于如何创建一个空的,非空的jQuery对象准备好追加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆