简体   繁体   English

如何在内部<span>或</span>内部转义 HTML<div> <span>标签</span>

[英]How to escape HTML inside <span> or <div> tag

<h4>    
  <span class="title">
      <textarea rows="4" cols="50"> How are you. </textarea>
  </span>
</h4>

How to consider <textarea rows="4" cols="50"> How are you. </textarea>如何考虑<textarea rows="4" cols="50"> How are you. </textarea> <textarea rows="4" cols="50"> How are you. </textarea> as a string and not HTML. <textarea rows="4" cols="50"> How are you. </textarea>作为字符串而不是 HTML。 I don't want HTML to render, I just want whatever comes into div or span it should render.我不希望 HTML 呈现,我只想要它应该呈现的 div 或 span 中的任何内容。

EXPECTED RESULT : <textarea rows="4" cols="50"> How are you. </textarea>预期结果: <textarea rows="4" cols="50"> How are you. </textarea> <textarea rows="4" cols="50"> How are you. </textarea>

With JQuery just use text() instead of html() to insert the code:使用 JQuery 只需使用 text() 而不是 html() 来插入代码:

 $('span').text('<textarea rows="4" cols="50"> How are you. </textarea>');
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h4> <span class="title"></span> </h4>

Using pure JavaScript, without any library, you can use a function to insert the code as a text node in some container.使用纯 JavaScript,无需任何库,您可以使用函数将代码作为文本节点插入某个容器中。

 function insertAsText( containerId, text ) { var container = document.getElementById( containerId ); var textNode = document.createTextNode( text ); container.appendChild( textNode ); } insertAsText( "myDiv", "<p><textarea>test</textarea></p>");
 <div id="myDiv"></div>

I just added an ID to the target textarea for convenience:为方便起见,我只是在目标textarea添加了一个 ID:

 console.log(document.getElementById('txtArea'));
 <h4> <span class="tip" 4 "=" cols="50" original-title="title"> Hi hello <span class="title"><textarea rows="4" cols="50" id="txtArea"> How are you. </textarea></span> </span> </h4>

You need to escape the html you want to show您需要转义要显示的 html

&lt; is <
&quot; is ""
&gt; is >

There is an online tool that can do this for you here , but you can find many scripts that can do it for in runtime.这里有一个在线工具可以为您执行此操作,但您可以找到许多可以在运行时执行此操作的脚本。

 <h4> <span class="title"> &lt;textarea rows=&quot;4&quot; cols=&quot;50&quot;&gt; How are you. &lt;/textarea&gt; </span> </h4>

Pure JavaScript:纯 JavaScript:

document.getElementsByClassName("title")[0].innerHTML = "<div>HI!</div>";
//Result will be "HI!"
document.getElementsByClassName("title")[0].innerText = "<div>HI!</div>";
//Result will be "<div>HI!</div>"

JQuery:查询:

$(".title").html("<div>HI!</div>");
//Result will be "HI!"
$(".title").text("<div>HI!</div>");
//Result will be "<div>HI!</div>"

As a final comment, those ways are get/set.作为最后的评论,这些方法是 get/set。

如何到达<span>内部的 html 标签</span><div>使用 Java 脚本?</div><div id="text_translate"><p> 我有一个放置在div内的span标签,如下所示:</p><pre> &lt;div onclick="doActiveCheckBox('color1')" id="sss" class="test form-check form-option form-check-inline mb-2"&gt; &lt;input class="test form-check-input" type="radio" name="color" id="color1" data-bs-label="colorOption" value="/تاریک" checked=""&gt; &lt;label class="form-option-label rounded-circle" for="color1"&gt;&lt;span style="border:inherit; border-block-color:purple;" class="form-option-color rounded-circle" style="background-image: url(/img/ProductColors/green1.jpg)"&gt;&lt;/span&gt;&lt;/label&gt; &lt;/div&gt;</pre><p> 我想在单击input标签时为span标签设置border ,并且也不想在span中使用任何id 。 我尝试了以下方法:</p><p> 1:</p><pre> var d=document.getElementById("sss").getElementsByTagName("span"); d.style.border = "thick solid #0000FF";</pre><p> 2:</p><pre> var d=document.getElementById("sss").getElementsByClassName(); for (var i = 1; i &lt;= d.length; i++) { d[1].style.border = "thick solid #0000FF"; }</pre><p> 但他们都没有正常工作? 那么有人会帮忙吗?</p></div> - How to reach a html <span> tag inside a <div> using Java Script?

暂无
暂无

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

相关问题 如何到达<span>内部的 html 标签</span><div>使用 Java 脚本?</div><div id="text_translate"><p> 我有一个放置在div内的span标签,如下所示:</p><pre> &lt;div onclick="doActiveCheckBox('color1')" id="sss" class="test form-check form-option form-check-inline mb-2"&gt; &lt;input class="test form-check-input" type="radio" name="color" id="color1" data-bs-label="colorOption" value="/تاریک" checked=""&gt; &lt;label class="form-option-label rounded-circle" for="color1"&gt;&lt;span style="border:inherit; border-block-color:purple;" class="form-option-color rounded-circle" style="background-image: url(/img/ProductColors/green1.jpg)"&gt;&lt;/span&gt;&lt;/label&gt; &lt;/div&gt;</pre><p> 我想在单击input标签时为span标签设置border ,并且也不想在span中使用任何id 。 我尝试了以下方法:</p><p> 1:</p><pre> var d=document.getElementById("sss").getElementsByTagName("span"); d.style.border = "thick solid #0000FF";</pre><p> 2:</p><pre> var d=document.getElementById("sss").getElementsByClassName(); for (var i = 1; i &lt;= d.length; i++) { d[1].style.border = "thick solid #0000FF"; }</pre><p> 但他们都没有正常工作? 那么有人会帮忙吗?</p></div> - How to reach a html <span> tag inside a <div> using Java Script? div标签内的跨度标签未出现 - Span tag inside the div tag is not appearing 在div内用span(例如)标签包裹单词,而div内有HTML标记 - Wrap words inside div with span (for example) tag, while the div has HTML tags inside 如何在div标签内获取跨度内部文本 - how to get span inner text inside div tag 如何在.html Jquery方法内的变量中添加跨度标签? - How to add a span tag to a variable inside a .html Jquery method? 排除div中第一个span标签的子元素 - Exclude children of the first span tag inside div 如何在div内获取带有标签的内部HTML? - How to get a tag inner html with is inside a div? div内span标签的onclick事件 - onclick event for span tag inside div 如何清除div内的跨度 - how to clear a span inside a div 如何在按钮标签内插入跨度标签? - how to insert a span tag inside a button tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM