jquery 如何将对象的代码移动到文本区域?

发布于 2024-10-02 13:23:23 字数 407 浏览 0 评论 0原文

我正在寻找一种将对象(横幅)作为字符串移动到文本区域的方法,以便用户可以轻松复制其内容:

var object_sting = $('<div>').append($('#customized_banner').clone()).remove().html();
$('#customized_banner_code').attr("innerHTML", object_sting);

我正在尝试用这两行来解决它。第一个结果是一个字符串,第二个应该将该字符串添加到文本区域中。不幸的是它没有添加它。

奇怪的是,两条线路本身都按照预期进行。第一个确实将对象作为字符串加载到变量中。如果我用字符串替换 object_string,第二个确实会将文本添加到文本区域中。

谢谢你的提示! 马库斯

Im looking for a way to move a object (a banner) as a string to a textarea so that the user has it easy to copy past its content:

var object_sting = $('<div>').append($('#customized_banner').clone()).remove().html();
$('#customized_banner_code').attr("innerHTML", object_sting);

I'm trying to solve it with these two lines. The first results in a string and the second should add the string into the textarea. It unfortunately dosen't add it.

Strange is that both lines in itself do what they are expected to. the first does load the object in the variable as string. And the second does add text into the textarea if I replace the object_string with string.

Thanks for your hint!
Markus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悟红尘 2024-10-09 13:23:23

你可以尝试:

$('#customized_banner_code').val(object_sting);

或者甚至

$('#customized_banner_code').val(escape(object_sting));

you can try:

$('#customized_banner_code').val(object_sting);

or even

$('#customized_banner_code').val(escape(object_sting));
初相遇 2024-10-09 13:23:23

您将 HTML 代码添加为文本区域内的代码。尝试将其添加为文本:

var object_sting = $('<div>').append($('#customized_banner').clone()).remove().html();
$('#customized_banner_code').val(object_sting);

You are adding the HTML code as code inside the textarea. Try to add it as text instead:

var object_sting = $('<div>').append($('#customized_banner').clone()).remove().html();
$('#customized_banner_code').val(object_sting);
冧九 2024-10-09 13:23:23

看起来你在获取字符串之前删除了整个内容。这样做:

var div = $('<div>').append($('#customized_banner').clone());
var object_sting = div.html();
div.remove();
$('#customized_banner_code').val( object_sting );

Looks like you delete the whole thing before you get the string. Do this:

var div = $('<div>').append($('#customized_banner').clone());
var object_sting = div.html();
div.remove();
$('#customized_banner_code').val( object_sting );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文