JavaScript 直接对 style 属性进行修改,来达到动态改变元素风格的目的。
假定元素(id1)的风格表单声明 :
style=”font-family:Arial;font-size:12px;”
则您就可以在 JavaScript 中读取和修改风格属性
document.getElementById(“id1″).style.fontFamily = “Geneva”;
document.getElementById(“id1″).style.fontSize = “14px”;
注意:CSS 属性自身是 camelCased 的大小写是驼峰式的,即第一个词的首字小写,随后的每个词首字大写,而不是用连字符“-”进行连接的;如document.getElementById (“id1″).style.font-family = “Geneva”;这样写是错误的。
getElementById getElementsByName getElementsByTagName 的用法与区别
getElementById:
语法: document.getElementById(id)
参数:id :必选项为字符串(String)
返回值:对象; 返回相同id对象中的第一个,如果无符合条件的对象,则返回 null
example:document.getElementById(“id1″).value;
getElementsByName:
语法: document.getElementsByName(name)
参数:name :必选项为字符串(String)
返回值:数组对象; 如果无符合条件的对象,则返回空数组
example:document.getElementsByName(“name1″)[0].value;
document.getElementsByName(“name1″)[1].value;
getElementsByTagName:
语法: object.getElementsByTagName(tagname) object可以是document或event.srcElement.parentElement等
参数:tagname:必选项为字符串(String)
返回值:数组对象; 如果无符合条件的对象,则返回空数组
example:document.getElementsByTagName(“p”)[0].childNodes[0].nodeValue;
document.getElementsByTagName(“p”)[1].childNodes[0].nodeValue;
拷贝至HTML页面中执行可看效果。
<CENTER><textarea name=”textar” cols=”80″ rows=”15″>
<BR>如果<P>这是段落,
<HR>那将只取得其中的文字
</textarea><BR>
<input type=”button” onclick=click3() value=”处理代码”></CENTER>
<script>
function click3(){
textar.value=textar.value.replace(/<.*?>/g,”").replace(/ /g,” “).replace(/\n/g,”")
}
</SCRIPT>
<script language=”javascript“>
<!–
function cleanWordHtml(html){
// Remove all SPAN tags
html = html.replace(/</?SPAN[^>]*>/gi, “”);
// Remove Class attributes
html = html.replace(/<(w[^>]*) class=([^ |>]*)([^>]*)/gi, “<$1$3″) ;
// Remove Style attributes
html = html.replace(/<(w[^>]*) style=”([^"]*)”([^>]*)/gi, “<$1$3″) ;
// Remove Lang attributes
html = html.replace(/<(w[^>]*) lang=([^ |>]*)([^>]*)/gi, “<$1$3″) ;
// Remove XML elements and declarations
html = html.replace(/<\??xml[^>]*>/gi, “”) ;
// Remove Tags with XML namespace declarations: <o:p></o:p>
html = html.replace(/</?w+:[^>]*>/gi, “”) ;
// Replace the
html = html.replace(/ /, ” ” );
// Transform <P> to <DIV>
var re = new RegExp(“(<P)([^>]*>.*?)(</P>)”,”gi”) ;
// Different because of a IE 5.0 error
html = html.replace( re, “<div$2</div>”);return html;
}
//–>
</script>