为什么 script 标签不能写成自关闭形式

2015-3-24 liyangweb.com 李杨 HTML

最近工作太忙了,也没有时间写博客,今天恰巧看到了这篇文章,也解决了我很长时间也没想明白的问题。转过来给大家分享下。。。

转自:http://blogread.cn/it/article/3985?f=wb

 

今天早上在 Stack Overflow 看到了这个问题:Why don't self-closing script tags work? 。答案给出的解释是,在 XHTML 的标准里面规定非EMPTY 标签不能使用自关闭形式。注意这里使用的是全大写的 EMPTY ,所以我不把它翻译为「空白」。

那么 EMPTY 到底是什么呢?写过 DTD 的人应该知道它是个关键字,用来指明一个标签的内容必须是空白,而不能包含文本内容或子节点。看看具体的例子就很容易明白了:

<!ELEMENT img EMPTY>
<!ATTLIST img   %attrs;
  src %URI; #REQUIRED
  alt %Text; #REQUIRED
  longdesc %URI; #IMPLIED
  height %Length; #IMPLIED
  width %Length; #IMPLIED
  usemap %URI; #IMPLIED
  ismap (ismap) #IMPLIED
  >

这是 img 标签的定义。 ELEMENT 关键字说明它是一个元素, EMPTY 关键字说明它的内容必须是空白。因此,我们可以使用自关闭形式:

<img src="image.png" alt="some image" />

留意 ATTLIST 里面声明了两个属性是 #REQUIRED 的,所以必须提供。

接下来我们再看看 script 标签的定义:

<!ELEMENT script (#PCDATA)>
<!ATTLIST script
  id ID #IMPLIED
  charset %Charset; #IMPLIED
  type %ContentType; #REQUIRED
  language CDATA #IMPLIED
  src %URI; #IMPLIED
  defer (defer) #IMPLIED
  xml:space (preserve) #FIXED 'preserve'
  >

可以看到 script 标签通过 (#PCDATA) 声明了它的内部允许包含 CDATA 数据,因此它不是一个带EMPTY 关键字的标签,也就不可能使用自关闭的写法。

总结一下:有空看看 Stack Overflow 还是挺有意思的。学习 DTD 的语法,并且看看 XHTML 1.0 Strict DTD 也会帮助你增加对 XHTML 的理解。

 

标签: JavaScript HTML

评论:

kifftradingco
2019-01-17 03:54
<a href="http://www.heslaatours.com/adidas-damian-lillard-basketball-shoes-runninge">adidas damian lillard basketball shoes</a> <a href="http://www.prostekrasna.com/nike-acg-air-mowabb-dark-obsidian-navy-zapatosu">nike acg air mowabb dark obsidian navy</a> <a href="http://www.altharmony.com/air-jordan-flight-9-gr%C3%A5-svart-hvit-chaussuresa">air jordan flight 9 gr氓 svart hvit</a> <a href="http://www.cosmicupcake.com/game-vinnie-sunseri-jersey-new-orleans-saints-43-home-black-nflf">game vinnie sunseri jersey new orleans saints 43 home black</a> <a href="http://www.btmtecled.com/adidas-superstar-flex-copper-white-runningg">adidas superstar flex copper white</a> <a href="http://www.torcgunstore.com/35-authentic-grant-hill-detroit-pistons-jersey-mens-adidas-nba-purple-throwback-1995-all-nflv">35 authentic grant hill detroit pistons jersey mens adidas nba purple throwback 1995 all</a>
kifftradingco http://www.kifftradingco.com/
surlion
2018-12-31 11:26
<a href="http://www.pascaltosi.com/ray-ban-rb2132-52mm-sunglassesr">ray ban rb2132 52mm</a> <a href="http://www.ensitele.com/mackage-black-wool-coat-mackager">mackage black wool coat</a> <a href="http://www.globalgtprc.com/lebron-13-vs-lebron-13-elite-sneakerf">lebron 13 vs lebron 13 elite</a> <a href="http://www.ironrivermi.com/nike-air-max-flair-mens-green-orange-footc">nike air max flair mens green orange</a> <a href="http://www.buranna.com/nike-sb-dunk-id-skox">nike sb dunk id</a> <a href="http://www.adamaccademia.com/bon-march%C3%A9-nike-flyknit-trainer-hommes-orange-sportsd">bon march茅 nike flyknit trainer hommes orange</a>
surlion http://www.surlion.com/
educatetouch
2018-12-31 04:20
<a href="http://www.grupreklami.com/adidas-stan-smith-bianca-vapour-steel-obuva">adidas stan smith bianca vapour steel</a> <a href="http://www.gruzovig.com/nike-air-pegasus-92-p%C3%BArpura-kit-schuhee">nike air pegasus 92 p煤rpura kit</a> <a href="http://www.gscottwild.com/bl%C3%A5-and-gul-zx-flux-chaussuresg">bl氓 and gul zx flux</a> <a href="http://www.gulsahiriadam.com/air-jordan-melo-m8-for-vente-michigan-obuvc">air jordan melo m8 for vente michigan</a> <a href="http://www.gymncard.com/curry-2-amarillo-and-azul-kurpesb">curry 2 amarillo and azul</a> <a href="http://www.hacelosimple.com/jordan-1-svart-toe-outlet-kurpese">jordan 1 svart toe outlet</a>
educatetouch http://www.educatetouch.com/
myfraimz
2018-12-31 01:31
<a href="http://www.lauraundleon.com/limited-eric-wood-youth-jersey-buffalo-bills-70-road-white-nflc">limited eric wood youth jersey buffalo bills 70 road white</a> <a href="http://www.michaelphenow.com/nike-foamposite-rare-new-runningf">nike foamposite rare new</a> <a href="http://www.kguides.com/zdeno-chara-33-boston-bruins-reebok-premier-alternate-jersey-small-nflv">zdeno chara 33 boston bruins reebok premier alternate jersey small</a> <a href="http://www.nvusforex.com/nike-air-jordan-vi-6-womens-shoes-new-2014-blue-trainersa">nike air jordan vi 6 womens shoes new 2014 blue</a> <a href="http://www.dinerwaresb.com/nike-air-max-2010-vert-bleu-scarpec">nike air max 2010 vert bleu</a> <a href="http://www.slipoverfx.com/asics-gel-lyte-v-dark-bl%C3%A5-light-gr%C3%A5-kurpesc">asics gel lyte v dark bl氓 light gr氓</a>
myfraimz http://www.myfraimz.com/
inspiresatis
2018-12-30 11:11
<a href="http://www.cengorde.com/nike-air-max-1-hvit-junior-chaussuresa">nike air max 1 hvit junior</a> <a href="http://www.danielledraws.com/asics-asics-gel-kayano-20-lilla-bl%C3%A5-butya">asics asics gel kayano 20 lilla bl氓</a> <a href="http://www.gkpassword.com/air-max-90-rosso-bianca-grigio-butyc">air max 90 rosso bianca grigio</a> <a href="http://www.mackzy.com/nike-roshe-run-metric-nero-kaufen-obuvf">nike roshe run metric nero kaufen</a> <a href="http://www.metinmilli.com/nike-sb-stefan-janoski-max-femmes-blanc-orange-kurpesa">nike sb stefan janoski max femmes blanc orange</a> <a href="http://www.momandpopop.com/nike-shox-deliver-blanc-argent-kitchen-kurpesf">nike shox deliver blanc argent kitchen</a>
[url=http://www.inspiresatis.com/]inspiresatis[/url]
orderollie
2018-12-29 21:16
<a href="http://www.acforlessllc.com/tom-brady-michigan-jersey-for-sale-nflu">tom brady michigan jersey for sale</a> <a href="http://www.thehodostore.com/adidas-boost-black-and-gold-skou">adidas boost black and gold</a> <a href="http://www.gandoff.com/air-jordan-6-womens-red-runninge">air jordan 6 womens red</a> <a href="http://www.langbenlang.com/nike-air-max-tricky-quilted-pack-zapatosu">nike air max tricky quilted pack</a> <a href="http://www.glennloovens.com/air-jordan-13-university-r%C3%B8d-alert-chaussuresa">air jordan 13 university r酶d alert</a> <a href="http://www.kamilotomotiv.com/nike-rams-18-cooper-kupp-white-youth-stitched-elite-jersey-nflc">nike rams 18 cooper kupp white youth stitched elite jersey</a>
orderollie http://www.orderollie.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1