jQuery将DOM元素作为字符串

问题描述:

说我有

$(":input[type=text]:first")

我如何获得

<input type="text" value="" size="28" maxlength="140" tabindex="1" placeholder="search" class="textbox" name="q" autocomplete="off">

假设我在SO上运行这个?

Assuming I run this on SO?

更新我不想调用 .parent(),因为我在父元素中有很多其他内容。

Update I don't want to call .parent() since I have lots of other stuff in the parent element.

一个老技巧:

var selector = ":input[type=text]:first";

var str = $(selector).clone().wrap('<div/>').parent().html();

更新您无需担心拨打 .parent()因为您正在使用原始选择器的孤立克隆。

Update You don't need to worry about calling .parent() since you're working with an orphaned clone of the original selector.