admin管理员组文章数量:1319466
I'm trying to get the current HTML from an element using Froala 2.4
My main reason is to be able to pare the original HTML from the new HTML to see if the user has changed anything, and if that case trigger a save-event.
Here's my current code
console.log($(this).froalaEditor('html.get'))
console.log($(this).data('froala.editor')._original_html);
And here's the output
<p spellcheck="false">TestString<b>Edited</b></p>
TestString<b>Original</b>
The problem is that I want to only retrieve this from the first outout:
TestString<b>Edited</b>
(I don't want the Paragraph-tag to be included, in this example)
I could make a short function to take care of that for me, but it seems like I'm missing something really obvious regarding the way I get the string from Froala.
Help really appreciated!
I'm trying to get the current HTML from an element using Froala 2.4
My main reason is to be able to pare the original HTML from the new HTML to see if the user has changed anything, and if that case trigger a save-event.
Here's my current code
console.log($(this).froalaEditor('html.get'))
console.log($(this).data('froala.editor')._original_html);
And here's the output
<p spellcheck="false">TestString<b>Edited</b></p>
TestString<b>Original</b>
The problem is that I want to only retrieve this from the first outout:
TestString<b>Edited</b>
(I don't want the Paragraph-tag to be included, in this example)
I could make a short function to take care of that for me, but it seems like I'm missing something really obvious regarding the way I get the string from Froala.
Help really appreciated!
Share Improve this question edited Dec 18, 2016 at 19:19 Tompina asked Dec 18, 2016 at 18:27 TompinaTompina 7871 gold badge11 silver badges27 bronze badges 1-
I have updated my answer supposing you need to get rid only of top-level
<p>
tags. – pttsky Commented Dec 18, 2016 at 19:56
1 Answer
Reset to default 7You probably need to store the contents of your editor somewhere:
var html = $(this).froalaEditor('html.get'); // <p spellcheck="false">TestStringEdited</p>
And then just strip out HTML tags, if I got you right. The simplest way is jQuery .text() method:
var text = $( html ).text(); // TestStringEdited
update
You need to strip only upper-level <p>
tags right? Here is a function that implements filtering that tags with help of jQuery. But you can rewrite it and make an array of exclusion tags, or call it recursively on each child element of content. And also, you can use regular expressions as an alternative.
function stripParagraphs( html ) {
var r = '';
$( html ).each(function() {
// test each higher-level tag to be <p>
if ($( this ).prop( 'tagName' ) === 'P') {
r += $( this ).html(); // add contents of <p> to result
} else {
r += this.outerHTML; // add the whole element to result
}
})
return r;
}
Live example.
one more update
Strip all top-level tags.
function stripTopLevelTags( html ) {
var r = '';
$( html ).each( function() {
r += $( this ).unwrap().html();
});
return r;
}
Live example
本文标签: javascriptFroalaget HTMLStack Overflow
版权声明:本文标题:javascript - Froala - get HTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742061402a2418609.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论