On 11/24/06, Mathias Bank <[EMAIL PROTECTED]> wrote:
> Hi,
>
> $(expr, context) is a really nice function, but it could be optimized.
> Until now, it can only be used, if context is a dom tree. But see this
> scenario:
>
> var html='';
> html += "<ul>";
> for (var i=0; i<10;i++) {
>   html += "<li>listing "+i+"</li>";
> }
> html+='</ul>';
>
> Now, it would be great, if you could apply every jQuery function like
> $('li',html).css("color", "red");

You can create a "Document tree" using the `$` function:

var foo = $("<div><p>blah</p></div>").find("p").css("color", "red").end();

Then append it to any element you want to:

$(document.body).append(foo);

> Until know, I have to create a dom element like
>
> $('body').append('<div id="helper"></div>');
> $('#helper').html(html);
> $('#helper li',html).css("color","red");
> html = $('#helper').html();
> $('#helper').remove();
>
>
> That does not look good at all. Is there a way to realize this
> behaviour in jQuery-Core?

Using the method described above your code can be simplified to:

$(html).find("li").css("color", "red").end().appendTo("#someSelector");

-- 
Choan
<http://choangalvez.nom.es/>

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to