简体   繁体   English

如何从contentEditable div获取html进入隐藏文本表单元素?

[英]How do I get the html from a contentEditable div to go inside of a hidden text form element?

I don't believe that you can use the input straight from a content editable div and submit it in a form, so I figured that I could just have a hidden text field and do something like this: 我不相信您可以直接使用内容可编辑div中的输入并以表单形式提交,因此我认为我可以只有一个隐藏的文本字段并执行以下操作:

var blog_post = $("#editable_div").html();
$('#hidden_text_area').val(blog_post);

Does anyone know of a way to do this? 有谁知道这样做的方法? It is not working with what I have. 它不适用于我所拥有的。 I thought that maybe the $('#hidden_text_area').val(blog_post); 我想也许是$('#hidden_text_area').val(blog_post); needed to be triggered by an event, but I tried it with a click and it didn't work. 需要由一个事件触发,但我尝试点击它并没有用。

$('#clicker').click(function(){
  $('#hidden_text_area').val(blog_post);
})

Try: 尝试:

$('#clicker').click(function(){
 var blog_post = $("#editable_div").html();
 $('#hidden_text_area').val(blog_post);
})

Because it does not look like your blog_post variable contains the html content when you click this button. 因为当您单击此按钮时,您的blog_post变量看起来不包含html内容。

please check alert($('#hidden_text_area')) is giving object or not . 请检查警报($('#hidden_​​text_area'))是否给予对象。 if it is not giving object then it has problem otherwise $('#hidden_text_area').val(blog_post); 如果它没有给对象那么它就有问题,否则$('#hidden_​​text_area')。val(blog_post); should work. 应该管用。

Seems to work well for me, however, please be aware that the html in the value attribute will be escaped: 似乎对我有用,但请注意,value属性中的html将被转义:

HTML: HTML:

<textarea id="content">
    <p style="background-color: blue;">Testing</p>
</textarea>
<input type="button" value="hit me" id="btn" />
<div id="parent">
    <input type="text" name="testinput" id="inputtest" value="" />
</div>

JS: JS:

$("#btn").click(function () {
    var html_contents = $("#content").html();
    $("#inputtest").val(html_contents);
});

如何使用 Javascript 来操作 ' <div contenteditable' from a website?< div><div id="text_translate"><p> 请帮忙,如何使用 javascript 更改(或获取)图像中的文本“19000”? 我需要知道它来自动填写一些登录网站等空白的任务。 谢谢你。 <a href="https://i.stack.imgur.com/DThmX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DThmX.jpg" alt="https://shopee.vn/buyer/login 这是一个我想抓取信息的网站。"></a></p><p> <a href="https://shopee.vn/buyer/login" rel="nofollow noreferrer">https://shopee.vn/buyer/login</a>这是我要爬取信息的网站。</p></div></div> - How can I use Javascript to manipulate text inside '<div contenteditable' from a website?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在具有contenteditable的div元素中获取标记文本的父元素? - How to get the parent element of marked text inside div element with contenteditable? 我有一个来自html表的行:$(row).text()。 如何将其输入到input type =“ hidden”元素中 - I have a row from a html table in the form : $(row).text(). How do I feed this into a input type=“hidden” element 如何使用属性contentEditable删除div中的HTML元素? - How to delete an HTML element inside a div with attribute contentEditable? 如何在另一个元素内将contenteditable div的内容呈现为HTML? - How can I render the content of a contenteditable div as HTML inside another element? 如果知道元素ID,如何在contenteditable div中选择(获取范围或选择对象)元素? - How do I select (get range or selection object) an element in a contenteditable div if I know the element ID? 获取可编辑div中的焦点图像元素 - Get the focused image element inside a contenteditable div 如何检测 contenteditable div 中的 HTML 更改? - How can I detect a HTML change inside a contenteditable div? 如何在 html contenteditable div 标签中获取选定的文本位置? - How to get the selected text position in a html contenteditable div tag? 如何检查文本 cursor 是否在 contenteditable div 内? - How can I check if the text cursor is inside of a contenteditable div? 如何使用 Javascript 来操作 ' <div contenteditable' from a website?< div><div id="text_translate"><p> 请帮忙,如何使用 javascript 更改(或获取)图像中的文本“19000”? 我需要知道它来自动填写一些登录网站等空白的任务。 谢谢你。 <a href="https://i.stack.imgur.com/DThmX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DThmX.jpg" alt="https://shopee.vn/buyer/login 这是一个我想抓取信息的网站。"></a></p><p> <a href="https://shopee.vn/buyer/login" rel="nofollow noreferrer">https://shopee.vn/buyer/login</a>这是我要爬取信息的网站。</p></div></div> - How can I use Javascript to manipulate text inside '<div contenteditable' from a website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM