① js 用getElementsByTagName怎麼獲取其中一個元素的style
<intputtype="text"name="vv"value=""style="height:100px;"/>
<script>
varh=document.getElementsByTagName("input")[0].style.height;
aler(h)
</script>
② 如何通過js獲取css外聯樣式表的樣式
1 首先你要在head中引用樣式表
2 既然你已經引用了 直接調用就行 沒有影響的,就跟用頁面內樣式是一樣的
③ JS怎樣才能獲得非行內樣式
你好,
JS獲取非行內樣式的值是一個比較常見的問題,特別是使用原生JS開發項目的時候,經常會用到,考慮到兼容性問題,主要會涉及到兩個方法:currentStyle和getComputedStyle。
functioncss(node,prop,val){
if(val){
//設置css屬性
node.style[prop]=val;
}else{
//讀取屬性
if(node.currentStyle){
returnnode.currentStyle[prop];
}else{
returngetComputedStyle(node,null)[prop];
}
}
}
上面的函數封裝了讀取和設置css屬性的功能,並且解決了兼容性問題,使用方法很簡單:
varbd=document.body;
//設置body的背景顏色
css(bd,'backgroundColor','#f00');//設置為紅色背景
//讀取body的背景顏色
css(bd,'backgroundColor');//rgb(255,0,0)
希望能解決你的問題。
④ js怎樣獲得table樣式
創建和插入例子,按需自改
/** * 創建表格 * id 為表格id * arr 為表格表頭 */ function createTable(id,arr){ var table = document.createElement('table'); table.setAttribute("id",id); table.setAttribute("className","TableLine");//設定樣式 table.setAttribute("width",'98%'); table.setAttribute("cellpadding",'3'); table.setAttribute("cellspacing",'0'); var row = table.insertRow(); row.style.setAttribute("backgroundColor","#e0e0e0"); for (var i = 0; i < arr.length; i++) { var col = row.insertCell(); if(i==0){ col.setAttribute("width",'3%'); } col.setAttribute("className","border:1px solid #9BC2E0;"); col.setAttribute("align","center"); col.style.fontSize="13px"; col.style.fontWeight="Bold";; //var style = document.createAttribute("styles"); //style.nodeValue = "font-size:large"; //col.setAttributeNode(style); col.innerHTML = arr[i]; } //alert(table.outerHTML); return table; }
/** * 向表格插入一行 */ function addRow(table,id,arr){ var row = table.insertRow(); row.setAttribute("id",id); row.onclick=function (){}; for(var i=0;i<arr.length;i++){ var col = row.insertCell(); col.innerHTML = arr[i]; //col.innerText = arr[i]; col.setAttribute("title",arr[i]); } }
⑤ 如何通過js獲取style裡面的所有樣式信息啊
<html>
<head>
<title></title>
</head>
<body>
<divstyle="display:none;font-size:15px;font-weight:bold;">
</div>
</body>
<script>
vardivStyle=docuemnt.getElementsByTagName('div');
conslole.log(divStyle[0].style.cssText);
</script>
</html>
獲取首個div的style