简体   繁体   English

将HTML代码转换为div或textarea内的纯文本

[英]Convert html code into plain text inside a div or textarea

might be a stupid question but nonetheless here it goes. 可能是一个愚蠢的问题,但尽管如此。 I am having difficulty converting html code into plain text to be viewed preferably inside a textarea form field, but a div will do. 我很难将html代码转换成纯文本,最好在textarea表单字段内查看,但是div可以。

Here is what I have found: 这是我发现的:

html: 的HTML:

<div class="box-checkbox-color">

        <input type="checkbox" name="Color:_Green"  id="sku0001-green" value="Yes" />
        <label for="sku0001-green"></label>
        <div style="display:none;" title="#" class="rawHTML-code-insert">This is where I would like to display raw HTML code BUT have it rendered as plain text instead of being read as markup, essentially need it to be presented so it can be copied and pasted</div>
    </div>


<div class="box-checkbox-color">

        <input type="checkbox" name="Color:_Green2"  id="sku0001-green2" value="Yes" />
        <label for="sku0001-green2"></label>
        <div style="display:none;" title="#" class="rawHTML-code-insert">This is where I would like to display raw HTML code BUT have it rendered as plain text instead of being read as markup, essentially need it to be presented so it can be copied and pasted into a form field to be sent to another .php page</div>
    </div>


<ul class="result"></ul>

Javascript: Javascript:

$('input[type="checkbox"]').click(function(){
    var title = $(this).closest('.box-checkbox-color').find('.rawHTML-code-insert').html();
    // If the checkbox is checked, add the item to the ul.
    if($(this).attr('checked')){
        var html = '<li title="' + title + '">' + title + '</li>';
        $('ul.result').append(html);
    } else {     
        // if the checkbox is unchecked, remove the item from the ul.
        $('li[title="' + title + '"]').remove();
    }
});

What I need to accomplish with this is to show/generate a specific block of code when the user checks a specific checkbox, but I would like that code to be readable as code rather than it being read as markup and executed as such. 我需要完成的是在用户选中特定的复选框时显示/生成特定的代码块,但是我希望该代码作为代码可读,而不是作为标记读取并执行。 Point is to provide the code so users can copy and paste it, and make modifications to div name, id's, values, etc... if they want to. 重点是提供代码,以便用户可以复制和粘贴它,并在需要时对div名称,id,值等进行修改。 And of course, different code will appear for each checkbox and be listed together in the ul class below. 当然,每个复选框将显示不同的代码,并在下面的ul类中一起列出。 I provided a js fiddle that should illustrate what I need. 我提供了一个js小提琴,可以说明我的需求。

I have provided a js fiddle example of what I'm talking about. 我提供了一个我正在谈论的js小提琴示例。

You need to replace the html reserved characters with their escape sequences. 您需要使用其转义序列替换html保留字符。 Here 'sa good doc on that. 是一个很好的文档。 jQuery's text(str) should do that for you. jQuery的text(str)应该为您做到这一点。

You ca use &lt; 您可以使用&lt; instead of < and &gt; 代替<&gt; instead of > . 而不是> here is an example to convert html code to plain text 这是将html代码转换为纯文本的示例

HTML 的HTML

<div class="box-checkbox-color">

        <input type="checkbox" name="Color:_Green"  id="sku0001-green" value="Yes" />
        <label for="sku0001-green"></label>
        <div style="display:none;" title="#" class="rawHTML-code-insert">&lt;div class="box-checkbox-color"&gt;

        &lt;input type="checkbox" name="Color:_Green"  id="sku0001-green" value="Yes" /&gt;
        &lt;label for="sku0001-green"&gt;&lt;/label&gt;
    &lt;/div&gt;</div>
    </div>


<div class="box-checkbox-color">

        <input type="checkbox" name="Color:_Green2"  id="sku0001-green2" value="Yes" />
        <label for="sku0001-green2"></label>
        <div style="display:none;" title="#" class="rawHTML-code-insert">&lt;div class="box-checkbox-color"&gt;

        &lt;input type="checkbox" name="Color:_Green"  id="sku0001-green" value="Yes" /&gt;
        &lt;label for="sku0001-green"&gt;&lt;/label&gt;
    &lt;/div&gt;</div>
    </div>


<ul class="result"></ul>

JS JS

$('input[type="checkbox"]').click(function(){
var title = $(this).closest('.box-checkbox-color').find('.rawHTML-code-insert').html();
// If the checkbox is checked, add the item to the ul.
if($(this).attr('checked')){
    var html = '<li title="' + title + '">' + title + '</li>';
    $('ul.result').append(html);
} else {     
     var html = '';
    $('ul.result').html(html);
}

}); });

here is fiddle 这是小提琴

i hope it can help you. 我希望它可以帮助您。

I've added data-id attributes to the .rawHTML-code-insert elements; 我已将data-id属性添加到.rawHTML-code-insert元素; these get used as the IDs of the LI elements, so they can be found easily for removal. 这些被用作LI元素的ID,因此可以轻松找到它们以进行删除。 The problem with my previous fiddle was that the special characters in the title were messing up the [title="'+title+'"]' selector. 我以前的小提琴的问题是标题中的特殊字符弄乱了[title="'+title+'"]'选择器。

HTML: HTML:

<div class="box-checkbox-color">

    <input type="checkbox" name="Color:_Green"  id="sku0001-green" value="Yes" />
    <label for="sku0001-green"></label>
    <div style="display:none;" title="#" class="rawHTML-code-insert" data-id="html1">This is where I would like to display raw <b>HTML</b> code BUT have it rendered as plain text instead of being read as markup, essentially need it to be presented so it can be copied and pasted</div>
    </div>


<div class="box-checkbox-color">

    <input type="checkbox" name="Color:_Green2"  id="sku0001-green2" value="Yes" />
    <label for="sku0001-green2"></label>
    <div style="display:none;" title="#" class="rawHTML-code-insert" data-id="html2">This is where I would like to display raw HTML code BUT have it rendered as plain text instead of being read as markup, essentially need it to be presented so it can be copied and pasted into a form field to be sent to another .php page</div>
</div>

<ul class="result"></ul>

JS: JS:

$('input[type="checkbox"]').click(function(){
    var el = $(this).closest('.box-checkbox-color').find('.rawHTML-code-insert');
    var title = el.html();
    var id = el.data('id');
    // If the checkbox is checked, add the item to the ul.
    if($(this).is(':checked')){
        var html = $("<li/>", {
            title: title,
            text: title,
            id: id
        });
        $('ul.result').append(html);
    } else {     
        // if the checkbox is unchecked, remove the item from the ul.
        $('li#' + id).remove();
    }
});

DEMO 演示

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM