javascript 元素类型“myelement”必须后跟属性规范,“>”或“/>”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8561916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Element type "myelement" must be followed by either attribute specifications, ">" or "/>
提问by Sameh Farahat
My script function is defined inside a JSF2 form on a XHTML page and the following part of the code causes a problem:
我的脚本函数是在 XHTML 页面上的 JSF2 表单中定义的,代码的以下部分会导致问题:
<script type="text/javascript">
function myFunction(){
var checkboxes = document.getElementsByName('mycheckboxes');
if(checkboxes.length > 0){
for (var i=0; i<checkboxes.length; i++){
var checkbox = checkboxes[i];
}
}
}
</script>
When trying to access the page in FireFox 8 it prints the exception:
当尝试访问 FireFox 8 中的页面时,它会打印异常:
Element type "checkboxes.length" must be followed by either attribute specifications, ">" or "/>"
元素类型“checkboxes.length”必须后跟属性规范,“>”或“/>”
what does this error mean?
这个错误是什么意思?
Another question: is the script is executed before the pages is rendered? Because my checkboxes is loaded in the page during render phase of page (using JSF <ui:repeat>
), so my guess is that I must make a condition to execute the code when the variable checkboxes is not null, am I right?
另一个问题:脚本是否在页面呈现之前执行?因为我的复选框是在页面的渲染阶段(使用 JSF <ui:repeat>
)加载到页面中的,所以我的猜测是当变量 checkboxes 不为空时,我必须制定一个条件来执行代码,对吗?
回答by Quentin
It looks like you are using XHTML and embedding scripts in the page.
看起来您正在使用 XHTML 并在页面中嵌入脚本。
XML doesn't have intrinsic CDATA sections, so <
means "start of tag" even inside a script element.
XML 没有内在的 CDATA 部分,因此<
即使在脚本元素中也意味着“标记的开始”。
If you were using real XHTML then you could represent it as <
, but then you couldn't serve the document as text/html
.
如果您使用的是真正的 XHTML,那么您可以将其表示为<
,但是您不能将文档作为text/html
.
Instead you can wrap the script content with an explicit CDATA section as described in the specificationwhile also taking care to follow the relevant text/html compatibility guidelines.
相反,您可以使用规范中描述的显式 CDATA 部分来包装脚本内容,同时还要注意遵循相关的text/html 兼容性指南。
You would probably be better off keeping your script in an external file and loading it with <script src="..."></script>
.
您最好将脚本保存在外部文件中并使用<script src="..."></script>
.
For that matter, you are likely to be better off using HTML instead of writing XHTML and then jumping through hoops to make it work in browsers that expect HTML.
就此而言,您可能最好使用 HTML,而不是编写 XHTML,然后跳过箍以使其在需要 HTML 的浏览器中工作。
回答by DRCB
If you write this script directly in the JSF source - you have to escape symbols like >
, <
, &
etc. with >
, &
...
如果直接在JSF源写这个剧本-你要逃一样的符号>
,<
,&
等用>
,&
...
Another way is to use CDATA
section:
另一种方法是使用CDATA
部分:
<script language="javascript">//<![CDATA[
/* here your script */
//]]>
</script>
See also:
也可以看看:
http://www.w3schools.com/xml/xml_cdata.asp
http://www.w3schools.com/xml/xml_cdata.asp
回答by PiTheNumber
"This error comes from not closing an attribute tag".
“此错误来自未关闭属性标签”。