gpt4 book ai didi

jquery - 在 JQuery 中获取节点的原始 HTML

转载 作者:技术小花猫 更新时间:2023-10-29 12:30:08 25 4
gpt4 key购买 nike

我用过$("#parent").html()获取 #parent 的内部 html , 但如何获取父级本身的 html?

用例是,我像这样抓取一个输入节点:

var field = $('input');

我希望能够使用类似 <input type='text'> 的内容获取该节点 ( field.html() ) 的原始 html ,但返回空。这可能吗?

最佳答案

或者您可以像这样创建添加一个 JQuery 函数:

jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
}

所以你可以这样做:

$('input').outerHTML();

$('input').outerHTML("new html");

感谢http://yelotofu.com/2008/08/jquery-outerhtml/

关于jquery - 在 JQuery 中获取节点的原始 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2779630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com