<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>setAttribute &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/setattribute/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Thu, 24 Jan 2013 07:22:38 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>
	<item>
		<title>Javascript的Attribute和setAttribute</title>
		<link>https://www.mudbest.com/javascript%e7%9a%84attribute%e5%92%8csetattribute/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Thu, 24 Jan 2013 07:19:06 +0000</pubDate>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Attribute]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[setAttribute]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1758</guid>

					<description><![CDATA[首先讲讲setAttribute，我们经常需要在JavaScript中给Element动态添加各种属性，这可以 ... <a title="Javascript的Attribute和setAttribute" class="read-more" href="https://www.mudbest.com/javascript%e7%9a%84attribute%e5%92%8csetattribute/" aria-label="阅读 Javascript的Attribute和setAttribute">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>首先讲讲setAttribute，我们经常需要在JavaScript中给Element动态添加各种属性，这可以通过使用setAttribute()来实现，这就涉及到了浏览器的兼容性问题。</p>
<pre class="brush: jscript; title: ; notranslate">
setAttribute(string name, string value)：
</pre>
<p>//增加一个指定名称和值的新属性，或者把一个现有的属性设定为指定的值。</p>
<p>1、样式问题<br />
setAttribute(&#8220;class&#8221;, value)中class是指改变&#8221;class&#8221;这个属性，所以要带引号。<br />
vName代表对样式赋值。<span id="more-1758"></span><br />
例如:</p>
<pre class="brush: jscript; title: ; notranslate">
var input = document.createElement(&quot;input&quot;);
input.setAttribute(&quot;type&quot;, &quot;text&quot;);
input.setAttribute(&quot;name&quot;, &quot;q&quot;);
input.setAttribute(&quot;class&quot;,bordercss);
</pre>
<p>输出时： ,即，input控件具有bordercss样式属性<br />
注意：class属性在W3C DOM中扮演着很重要的角色，但由于浏览器差异性仍然存在。<br />
使用setAttribute(&#8220;class&#8221;, vName)语句动态设置Element的class属性在firefox中是行的通的，但在IE中却不行。因为使用IE内核的浏览器不认识&#8221;class&#8221;，要改用&#8221;className&#8221;；<br />
同样，firefox 也不认识&#8221;className&#8221;。所以常用的方法是二者兼备：</p>
<pre class="brush: jscript; title: ; notranslate">
element.setAttribute(&quot;class&quot;, value); //for firefox
element.setAttribute(&quot;className&quot;, value); //for IE
</pre>
<p>2、方法属性等问题<br />
例如：<br />
var bar = document.getElementById(&#8220;testbt&#8221;);<br />
bar.setAttribute(&#8220;onclick&#8221;, &#8220;javascript:alert(&#8216;This is a test!&#8217;);&#8221;);<br />
这里利用setAttribute指定e的onclick属性，简单，很好理解。<br />
但是IE不支持，IE并不是不支持setAttribute这个函数，而是不支持用setAttribute设置某些属性，例如对象属性、集合属性、事件属性，也就是说用setAttribute设置style和onclick这些属性在IE中是行不通的。</p>
<p>为达到兼容各种浏览器的效果，可以用点符号法来设置Element的对象属性、集合属性和事件属性。</p>
<pre class="brush: jscript; title: ; notranslate">
document.getElementById(&quot;testbt&quot;).className = &quot;bordercss&quot;;
document.getElementById(&quot;testbt&quot;).style.cssText = &quot;color: #00f;&quot;;
document.getElementById(&quot;testbt&quot;).style.color = &quot;#00f&quot;;
document.getElementById(&quot;testbt&quot;).onclick= function () { alert(&quot;This is a test!&quot;); }
</pre>
<p>由此延伸的问题:</p>
<p>一个input的text,当将html赋值为某个div的innerHTML时,遇到一个现象,当在firefox下时(IE下不存在此问题),赋值后的innerHTML里不含有value,即当你在文本框输入内容后,<br />
你想将</p>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=text name=input1 value=”aa”&gt;
</pre>
<p>赋值给div时,只会得到</p>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=text name=input1&gt;
</pre>
<p>这里总是会将value清除.</p>
<p>这时,setAttribute起作用了,在input内加上:onkeyup=&#8221;this.setAttribute(&#8216;value&#8217;,this.value)&#8221;,即动态的将input控件加上value值,这时再将文本框赋值给div,value将不会被清空.</p>
<p>而对于Attribute一般可用来获取属性的值，例如</p>
<pre class="brush: jscript; title: ; notranslate">
document.getElementById(&quot;testbt&quot;).Attribute&#x5B;'src'].value;
</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
