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

发布于 2024-08-12 16:48:06 字数 533 浏览 5 评论 0原文

这是我的代码:

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

W3C 标记验证器不喜欢这样。它不希望我的 JavaScript 代码中包含 HTML 标签。如果我尝试这样做,它会产生以下错误消息:

字符“<”是分隔符的第一个字符,但作为数据出现

如果我将包含 HTML 标记的字符串传递给 document.getElementById('myElement').innerHTML,如何解决此问题,同时确保我的页面不会混乱?

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. It doesn't want HTML tags inside my JavaScript code. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我要还你自由 2024-08-19 16:48:06

您可以将函数包装在文档中其他位置的单独 标记内,并使用 ...

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

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

要解决此问题,我们可以执行以下两种操作之一。最简单的方法,特别是如果 Javascript 包含的不仅仅是一两行,就是使 Javascript 位于页面外部,导致它们在脚本标记之间不存在,从而停止页面验证。

如果只有一两行,那么可能不值得制作外部脚本,因此您需要将内容保留在脚本标记之间,并告诉验证器将忽略它。我们通过将 Javascript 代码放置在 CDATA 标记中来实现此目的,如下所示...

You could wrap your functions inside separate <script>...</script> tags somewhere else in the document, and there use ...

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

From 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.

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 ...

谜兔 2024-08-19 16:48:06

有很多方法可以到达那里。

  1. 使用 << 代替 <
    使用 >> 代替 >
  2. 获取图像的 id,例如“image1 “, 然后
    document.getElementById("image1").onmouseover = showDescription(
    '文本', '其中包含 HTML 标签的文本
    更多文本');

希望这有效。

There are many ways to get there.

  1. Use < or < instead of <
    Use > or > 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');

Hope this works.

浅沫记忆 2024-08-19 16:48:06
onmouseover="showDescription('Text', 'Text with HTML tags in them<br />More text');" 

与所有属性值一样,您必须对 &< 和属性分隔符(此处为 ")进行 HTML 编码。属性值中的 JavaScript 没有区别;HTML 属性值在 JavaScript 查看之前就被解码了,

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

这与内容为 CDATA 的

onmouseover="showDescription('Text', 'Text with HTML tags in them\x3Cbr />More text');" 
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.

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

This is in contrast to a <script> element, whose contents are CDATA and thus not &-escaped in HTML4. In XHTML there are no CDATA elements; 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. 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');" 
我是有多爱你 2024-08-19 16:48:06

< 替换为 %3C,将 > 替换为 %3E,并在输出内容时使用 unescape。

这不会验证:

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

这给出了相同的结果并验证:

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

Replace < by %3C and > by %3E and use unescape when outputting the contents.

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'); 
}
情丝乱 2024-08-19 16:48:06

将其放入

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

稍后在您的 HTML 中:

<a href="#">
    <img src="myimage.jpg" 
     onmouseover="showDescription(
          'Text', myText);" 
     onmouseout="revertDescription();" 
     alt="Image description">

How about putting this within a <script ...> block:

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

And later in your HTML:

<a href="#">
    <img src="myimage.jpg" 
     onmouseover="showDescription(
          'Text', myText);" 
     onmouseout="revertDescription();" 
     alt="Image description">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文