jQuery .html()不复制文本区域或输入的内容 [英] jQuery .html() does not copy contents of textareas or inputs

查看:102
本文介绍了jQuery .html()不复制文本区域或输入的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用elem.html()复制元素的内容,但其中不包括inputstextareas的内容.

I'm trying to copy the contents of an element using elem.html() but it's not including the contents of inputs or textareas.

这是一个示例(尝试在框中输入文字,然后单击复制"): http://jsfiddle.net /gAMmr/2/

Here's an example (try writting in the boxes then click "Copy"): http://jsfiddle.net/gAMmr/2/

是否可以复制所有信息?

Is there a way of copying all info?

这些是我到目前为止尝试过的方法:

These are the approaches I have tried so far:

  • elem.clone()-不适合我的任务,因为它会复制 元素本身
  • elem.children().clone()-错过了文本节点
  • elem.contents().clone()-不包括文本区域内容
  • elem.clone() - not suitable for my task because it copies the element itself
  • elem.children().clone() - misses out text nodes
  • elem.contents().clone() - doesn't include the textarea contents

在每个浏览器中结果似乎都不同.我正在使用Chrome.

The results seem to be different in each browser. I'm using Chrome.

推荐答案

$("button").click(function () {
    $("#2").html($("#1").html());
    $("#1").find("input").each(function(idx) {
        $("#2").find("input").eq(idx).val($(this).val());
    });
    $("#1").find("textarea").each(function(idx) {
        $("#2").find("textarea").eq(idx).val($(this).val());
    });
});

http://jsfiddle.net/gAMmr/5/

这篇关于jQuery .html()不复制文本区域或输入的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆