admin管理员组

文章数量:1334923

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

本文标签: javascript(Simple) Are HTML attributes allowed to have spaces between assignmentsStack Overflow