JavaScript可以在[CDATA [标记]之外使用吗? [英] Can JavaScript function outside of the [CDATA[ tag?

查看:63
本文介绍了JavaScript可以在[CDATA [标记]之外使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的阅读,必须将SVG中的JavaScript放入[CDATA [标记(类型标识现在显然是可选的):

From what I read, JavaScript within an SVG must be put into a [CDATA[ tag (the type designation is apparently now optional):

<script type="application/javascript"> 
<![CDATA[

 (JavaScipt stuff here)

]]> 
</script>

但是,即使看起来正在使用JavaScript方法,以下标记/代码也可以在没有该标记的情况下工作:

However, the following markup/code works without that tag even though it looks like JavaScript methods are being used:

<svg width="400px" height="400px" viewBox="0 0 400 400">
<rect id="rect1" x="160" y="10" width="60" height="60"  fill="blue"
onmouseenter="evt.target.setAttribute('fill', 'red');"
onmouseout="evt.target.setAttribute('x', '0');"
/>
</svg>

那么,evt.target.setattribute命令是否全部为SVG?我知道这是一个超级新手问题...但是我很难确定SVG结束和JavaScript开始的位置.关于该划界的文档非常粗略,我想在需要学习另一种语言之前先了解我在SVG中可以做的全部工作.非常感谢您的澄清.而且,如果有人知道在该主题上非常清楚的参考文献,我很想知道.

So, are the evt.target.setattribute commands all SVG? I know this is a super newbie question...but I am having a hard time establishing where SVG ends and JavaScript begins. Documentation is very sketchy on the delineation, and I want to learn the total extent of what I can do in SVG before needing to learn another language. Thanks much for any clarification. And if anyone knows of a reference that is very clear on the subject, I'd love to know.

推荐答案

CDATA节是XML文档的功能.它不是html的一部分.鉴于您的SVG示例没有名称空间,因此您似乎在html中使用SVG.因此,这里的CDATA既不需要也不有用.

A CDATA section is a feature of XML documents. It's not part of html. You seem to be using SVG in html given that your SVG example has not namespaces so CDATA is neither required nor useful there.

在独立的SVG中,您需要定义名称空间,即xmlns ="http://www.w3.org/2000/svg",在此您需要CDATA来防止javascript中的各种字符,例如<被解释为标签的开头.其他字符也有问题.

In standalone SVG you need namespaces defined i.e. xmlns="http://www.w3.org/2000/svg" and there you need CDATA to prevent various characters in javascript e.g. < being interpreted as the beginning of a tag. Other characters are problematic too.

这篇关于JavaScript可以在[CDATA [标记]之外使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆