开发者

Create jquery item from string without actually inserting into DOM?

开发者 https://www.devze.com 2023-01-16 18:56 出处:网络
Is it possible to create a jquery object that I can perform jq开发者_StackOverflowuery functions on from just a string representation?

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

精彩评论

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