开发者

Create jquery item from string without actually inserting into DOM?

Is it possible to create a jquery object that I can perform jq开发者_StackOverflowuery functions on from just a string representation?

I.e.

var item = '<div>hello</div>';
???
alert(item.html());

I think I could do it by adding it to the DOM using append(), then reselecting it, but i dont really want to do that as it seems horribly ineffcient.

EDIT: Thanks for all the replies


Try this:

var item = '<div>hello</div>';
alert($(item).html());

However the html() function displays the inner html of the object created.


Although not a great solution, you can do the following:

var item = "<div>hjello</div>";
var itemSelector = $(item);
alert(itemSelector.html());

^_^

edit:

var item = "<div>hjello</div>";
alert($(item).html());


Try:

var item = $('<div>');
item.text('hello');

alert(item.text);

item is not a part of the DOM, you can manipuate it the way you like and append it where you like when you feel like it.


var item = $.create('<div>hello</div>');

try that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜