本文介绍了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.

这篇关于jQuery将DOM元素作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 23:59
查看更多