开发者

How to Convert an array of jQuery Objects to an HTML String

In the jQuery Infinite Carousel, it uses .clone() to do the infinite effect. This works great unless the code it's cloning开发者_如何学C has HTML5 Elements. IE7 and IE8 have trouble applying HTML5 Element-specific CSS rules to cloned or otherwise post-page-load inserted elements.

The innerShiv JavaScript plugin inserts the elements in a way that IE7 and IE8 will render just fine with the appropriate CSS.

The problem is that innerShiv takes an HTML string as a parameter, but the jQuery .clone() method returns an array of jQuery objects.

In order to use the two together, I need to convert the output from .clone() into an HTML string that innerShiv will be able to parse.

Any thoughts on how this could be done?


HTML:

<p class="foo"><canvas class="bar"></canvas></p>

JavaScript: [Ref]

// grab the object, with HTML 5 element(s).
var p = $('p');

// clone it
var c = p.clone();

// grab the inner html (wrap it so we can get the HTML)
var html = $('<div>').append(c).html();

// alert us of the contents
alert(html);

Demo:

http://jsfiddle.net/bradchristie/tDFYn/


Use .clone().html();, it will convert the objects to a string. Take a look: http://jsfiddle.net/9k2LS/

-edit-

Reconsidered: Indeed @Jazzerus, why use .clone()? .html() without clone() will do.


var html = $.parseHTML(yourHtmlString); //Returns an Array of HTML Objects

//some operations on html like $(html).find() ...

html  = $('<div>').append(html).html();  // Returns string (string of html)
console.log(html);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜