简体   繁体   English

在 JavaScript 字符串中使用 HTML 标签,同时遵守 W3C 规则

[英]Using HTML tags in JavaScript strings while complying with W3C rules

Here's my code:这是我的代码:

<a href="#">
    <img src="myimage.jpg" 
     onmouseover="showDescription(
          'Text', 'Text with HTML tags in them<br />More text');" 
     onmouseout="revertDescription();" 
     alt="Image description">

The W3C Markup Validator doesn't like this. W3C 标记验证器不喜欢这样。 It doesn't want HTML tags inside my JavaScript code.它不需要我的 JavaScript 代码中的 HTML 标签。 Here's the error message it produces if I attempt this:如果我尝试这样做,这是它产生的错误消息:

character "<" is the first character of a delimiter but occurred as data字符“<”是定界符的第一个字符,但作为数据出现

How can I fix this while making sure that my page doesn't mess up if I pass the HTML tag-containing string to document.getElementById('myElement').innerHTML ?如果我将包含 HTML 标签的字符串传递给document.getElementById('myElement').innerHTML ,我该如何解决这个问题同时确保我的页面不会混乱?

You could wrap your functions inside separate <script>...</script> tags somewhere else in the document, and there use ... 您可以将函数包装在文档中其他位置的单独<script>...</script>标记内,并使用...

<script>
//<![CDATA[
    ...code...
//]]>
</script>

From http://javascript.about.com/library/blxhtml.htm : 来自http://javascript.about.com/library/blxhtml.htm

To fix this problem wer can do one of two things. 要解决这个问题,可以做两件事之一。 The simplest way, particularly if the Javascript contains more than just one or two lines, is to make the Javascript external to the page resulting in their being nothing between the script tags to stop the page validating. 最简单的方法,特别是如果Javascript包含的不仅仅是一行或两行,就是使页面外部的Javascript导致它们在脚本标记之间无效,以阻止页面验证。

If it is just one or two lines then it is probably not worth making an external script so you will want to leave the content between the script tags and tell the validator that this is to be ignored. 如果它只是一行或两行,则可能不值得制作外部脚本,因此您需要将内容保留在脚本标记之间,并告诉验证者这将被忽略。 We do this by placing the Javascript code within a CDATA tag like this ... 我们通过将Javascript代码放在像这样的CDATA标签中来实现这一点......

There are many ways to get there. 有很多方法可以实现这一目标。

  1. Use &#60; 使用&#60; or &lt; 或者&lt; instead of < 而不是<
    Use &#62; 使用&#62; or &gt; 或者&gt; instead of > 而不是>
  2. Get a id to the image, such as "image1", then document.getElementById("image1").onmouseover = showDescription(
    'Text', 'Text with HTML tags in them<br />More text');
    获取图像的id,例如“image1”,然后是document.getElementById("image1").onmouseover = showDescription(
    'Text', 'Text with HTML tags in them<br />More text');
    document.getElementById("image1").onmouseover = showDescription(
    'Text', 'Text with HTML tags in them<br />More text');

Hope this works. 希望这有效。

onmouseover="showDescription('Text', 'Text with HTML tags in them<br />More text');" 

Like with all attribute values, you must HTML-encode & , < , and the attribute delimiter ( " here). The fact that it's JavaScript inside the attribute value makes no difference; the HTML attribute value is decoded before JavaScript gets a look at it. 与所有属性值一样,您必须HTML编码&<和属性定界符( "此处)。事实上,它在属性值中的JavaScript没有区别; HTML属性值在JavaScript查看之前被解码。

onmouseover="showDescription('Text', 'Text with HTML tags in them&lt;br />More text');" 

This is in contrast to a <script> element, whose contents are CDATA and thus not & -escaped in HTML4. 这与<script>元素形成对比, <script>元素的内容为CDATA ,因此不在HTML4中进行& #cacaped。 In XHTML there are no CDATA elements; 在XHTML中没有CDATA元素; you can add a <![CDATA[ section to make XHTML behave the same, but it's usually simpler for both script elements and event handler attributes to just avoid the problem by never using a & or < character. 你可以添加一个<![CDATA[部分来使XHTML表现相同,但是对于脚本元素和事件处理程序属性来说通常更简单,因为从不使用&<字符来避免这个问题。 In a string literal another escape is available which you can use to get around this: 在字符串文字中,可以使用另一个转义,您可以使用它来解决此问题:

onmouseover="showDescription('Text', 'Text with HTML tags in them\x3Cbr />More text');" 

Replace < by %3C and > by %3E and use unescape when outputting the contents. < by %3C>替换为%3E并在输出内容时使用unescape。

This won't validate: 这不会验证:

function(){
return ('<b> bold </b>');
}

This gives the same results and validates: 这给出了相同的结果和验证:

function(){
return unescape('%3Cb%3E bold %3C/b%3E'); 
}

How about putting this within a <script ...> block: 将它放在<script ...>块中怎么样:

var myText = 'Text with HTML tags in them<br />More text';

And later in your HTML: 稍后在您的HTML中:

<a href="#">
    <img src="myimage.jpg" 
     onmouseover="showDescription(
          'Text', myText);" 
     onmouseout="revertDescription();" 
     alt="Image description">
<div id="your-text"></div>
document.getElementById('your-text').innerHTML = yourText;

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

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