国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

原生js和jQuery隨意改變div屬性style的名稱和值

2024-05-06 16:10:02
字體:
來源:轉載
供稿:網友
用原生js和jQuery實現改變隨意改變div屬性style的名稱和值的結果,這個實例比較實用,新手朋友們可以看看
一些簡單的例子,用原生js和jQuery實現隨意改變div屬性,和重置。代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>函數傳參,改變Div任意屬性的值</title>
<style type="text/css">
body,p{margin:0;padding:0;}
body{color:#333;font:12px/1.5 Tahoma;padding-top:10px;}
#outer{width:300px;margin:0 auto;}
p{margin-bottom:10px;}
button{margin-right:5px;}
label{width:5em;display:inline-block;text-align:right;}
input{padding:3px;font-family:inherit;border:1px solid #ccc;}
#div1{color:#fff;width:180px;height:180px;background:#000;margin:0 auto;padding:10px;}
</style>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>函數傳參,改變Div任意屬性的值</title>
<style type="text/css">
body,p{margin:0;padding:0;}
body{color:#333;font:12px/1.5 Tahoma;padding-top:10px;}
#outer{width:300px;margin:0 auto;}
p{margin-bottom:10px;}
button{margin-right:5px;}
label{width:5em;display:inline-block;text-align:right;}
input{padding:3px;font-family:inherit;border:1px solid #ccc;}
#div1{color:#fff;width:180px;height:180px;background:#000;margin:0 auto;padding:10px;}
</style>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
/*var changeSytle = function (elem,name,value){
elem.style[name] = value;
}
window.onload = function (){
var oDiv = document.getElementById("div1");
var oBtn = document.getElementsByTagName("button");
var oInput = document.getElementsByTagName("input");
oBtn[0].onclick = function (){
changeSytle (oDiv,oInput[0].value,oInput[1].value)
} ,
oBtn[1].onclick = function (){
oDiv.removeAttribute("style");
}
} */原生js部分實現


$(function(){
$("button:first").click(function(){
var styleName= $("#outer").find("input:first").val();
var styleVal = $("#outer").find("input:last").val();
$("#div1").css(styleName,styleVal);
})
$("button:last").click(function(){
$("#div1").removeAttr("style");
})
})

</script>

</head>
<body>
<div id="outer">
<p><label>屬性名:</label><input type="text" value="background" name="styleName" /></p>
<p><label>屬性值:</label><input type="text" value="blue" name="val" /></p>
<p><label></label><button>確定</button><button>重置</button></p>
</div>
<div id="div1">在上方輸入框輸入"屬性名"及"屬性值",點擊確定按鈕查看效果。</div>

</body>
</html>
$(function(){
$("button:first").click(function(){
var styleName= $("#outer").find("input:first").val();
var styleVal = $("#outer").find("input:last").val();
$("#div1").css(styleName,styleVal);
})
$("button:last").click(function(){
$("#div1").removeAttr("style");
})
})

</script>

</head>
<body>
<div id="outer">
<p><label>屬性名:</label><input type="text" value="background" name="styleName" /></p>
<p><label>屬性值:</label><input type="text" value="blue" name="val" /></p>
<p><label></label><button>確定</button><button>重置</button></p>
</div>
<div id="div1">在上方輸入框輸入"屬性名"及"屬性值",點擊確定按鈕查看效果。</div>

</body>
</html>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 平乡县| 上高县| 西昌市| 长岭县| 明星| 桓仁| 崇仁县| 梁河县| 宁都县| 巴青县| 贵定县| 彭州市| 思南县| 花莲县| 鲁甸县| 盐源县| 博湖县| 张北县| 子洲县| 台前县| 深州市| 清水县| 神农架林区| 罗江县| 运城市| 黎川县| 金昌市| 彩票| 孝感市| 荔浦县| 吴桥县| 菏泽市| 崇仁县| 鸡泽县| 依兰县| 都兰县| 邻水| 泾川县| 论坛| 汤原县| 安陆市|