最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - (Simple) Are HTML attributes allowed to have spaces between assignments - Stack Overflow

programmeradmin27浏览0评论

I'm doing some attribute parsing as a string, so I need to know how spaces are used in HTML/XHTML element attributes:

  <div id='myid' width='150px' />

Is this also correct?

  <div id = 'myid' width = '150px' />

If anyone knows other ways of iterating through attributes and their values using JavaScript, I'd be interested to know.

I'm doing some attribute parsing as a string, so I need to know how spaces are used in HTML/XHTML element attributes:

  <div id='myid' width='150px' />

Is this also correct?

  <div id = 'myid' width = '150px' />

If anyone knows other ways of iterating through attributes and their values using JavaScript, I'd be interested to know.

Share Improve this question edited Feb 1, 2015 at 16:43 Deduplicator 45.7k7 gold badges72 silver badges123 bronze badges asked May 6, 2009 at 6:18 Robin RodricksRobin Rodricks 114k147 gold badges414 silver badges617 bronze badges 2
  • You might want to specify what language you're trying to do the parsing in - if in C, C#, JavaScript. If JavaScript, are you perhaps trying to your own HTML document from within a browser, or offline? – Shalom Craimer Commented May 6, 2009 at 6:27
  • Note that the width attribute is (a) deprecated and (b) takes either an integer or percentage - don't specify px in HTML (but always specify units in CSS). The validator won't pick this up, because the DTD doesn't express this rule (and I don't think DTDs are capable of doing that either) – Quentin Commented May 6, 2009 at 8:35
Add a ment  | 

2 Answers 2

Reset to default 8

Yes, both are correct. Rather than string parsing, you'll want to use the DOM. Check out jquery.

The spaces are allowed. If you are parsing attributes why don't you let the browser parse? If you are using innerHTML than you get elements that have an attribute list

发布评论

评论列表(0)

  1. 暂无评论