css - Why does styling TDs in jquery make the markup insane? -


i'm trying add simple border table cells in table.

it's important markup remains simple other functionality have in place work.

let's style tds this:

$('td').css('border', '1px solid #000'); 

this ends result:

<td style=​"border-top-width:​ 1px;​ border-right-width:​ 1px;​ border-bottom-width:​ 1px;​ border-left-width:​ 1px;​ border-top-style:​ solid;​ border-right-style:​ solid;​ border-bottom-style:​ solid;​ border-left-style:​ solid;​ border-top-color:​ rgb(0, 0, 0)​;​ border-right-color:​ rgb(0, 0, 0)​;​ border-bottom-color:​ rgb(0, 0, 0)​;​ border-left-color:​ rgb(0, 0, 0)​;​ ">​…​/td>

classes wouldn't appropriate i'm trying do. why cells being formatted in ridiculous way?

this how browsers handle shorthand css properties; browsers may implement human-readable representation differently internal representation, in reality,

border: 1px solid #000 

is shorthand for

border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #000; border-right-color: #000; border-bottom-color: #000; border-left-color: #000; 

with number of possible representations of #000.

i tested in both chrome , safari (both webkit browsers), , setting property directly in dom (element.style.border = '1px solid #000') had result. behavior of webkit, not jquery.

furthermore, great example underscore fact dom different markup, , ought treated differently. in designmode/contenteditable scenario (which really reasonable use of direct styling instead of css classes), snatching innerhtml representation of element whole host of potential risks. you'll see other manifestations of in older versions of ie, "markup" representations of dom insane. it's not uncommon see stuff like:

<div class="foo">...</div> 

become:

<div class="foo" _jquery12903579="qwertyuiop" haslayout=true etc etc etc>...</div> 

and dom , markup not same thing, it's important realize attribute , property not same thing. ultimately, jquery.css method implemented assigning values properties of element.style (eg collection.css({ border: '1px solid #000' }) equivalent collection.each(function() { this.style.border = '1px solid #000' }); while collection.attr('style', 'border: 1px solid #000') equivalent collection.each(function() { this.setattribute('style', 'border: 1px solid #000'); }). setting attribute rather properties, you're doing retroactively editing html markup. can lead unpredictable results if have properties set conflict.

also note using attr('style', ...), overriding all of elements' inline styles.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -