jQuery實現(xiàn)隨意改變div任意屬性的名稱和值(部分原生js實現(xiàn))
更新時間:2013年05月28日 17:48:34 作者:
用原生js和jQuery實現(xiàn)隨意改變div屬性和重置,在輸入框輸入“屬性名”及“屬性值”,點擊確定按鈕查看效果,感興趣的你可不要錯過了哈
一些簡單的例子,用原生js和jQuery實現(xiàn)隨意改變div屬性,和重置。代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>函數(shù)傳參,改變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>函數(shù)傳參,改變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部分實現(xiàn)
$(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>
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>函數(shù)傳參,改變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>函數(shù)傳參,改變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部分實現(xiàn)
$(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>
您可能感興趣的文章:
- JS實現(xiàn)DIV容器賦值的方法
- 原生js和jQuery隨意改變div屬性style的名稱和值
- js使用循環(huán)清空某個div中的input標(biāo)簽值
- js動態(tài)設(shè)置div的值下例子
- 原生js實現(xiàn)改變隨意改變div屬性style的名稱和值的結(jié)果
- js 為label標(biāo)簽和div標(biāo)簽賦值的方法
- JS值當(dāng)前DIV的ID值的代碼
- jQuery給div,Span, a ,button, radio 賦值與取值
- jquery設(shè)置text的值示例(設(shè)置文本框 DIV 表單值)
- 利用JQuery+EasyDrag 實現(xiàn)彈出可拖動的Div,同時向Div傳值,然后返回Div選中的值
- JavaScript和JQuery獲取DIV值的方法示例
相關(guān)文章
uniapp實現(xiàn)h5、app與微信小程序三端pdf文件下載和預(yù)覽功能
作為使用uni-app的小白來說,嘗試了好幾種方法,終于得到了我想要的效果,下面這篇文章主要給大家介紹了關(guān)于uniapp實現(xiàn)h5、app與微信小程序三端pdf文件下載和預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下2022-12-12
用js來定義瀏覽器中一個左右浮動元素相對于頁面主體寬度的位置的函數(shù)
用js來定義瀏覽器中一個左右浮動元素相對于頁面主體寬度的位置的函數(shù) 函數(shù)的參數(shù):elem元素、頁面主體寬度(如:990、950寬)、elem與頁面主體DIV的左邊距2012-01-01
javascript設(shè)計模式 – 命令模式原理與用法實例分析
這篇文章主要介紹了javascript設(shè)計模式 – 命令模式,結(jié)合實例形式分析了javascript命令模式相關(guān)概念、原理、用法及操作注意事項,需要的朋友可以參考下2020-04-04
JavaScript實現(xiàn)頁面跳轉(zhuǎn)的5種方法總結(jié)
在前臺開發(fā)中會涉及頁面跳轉(zhuǎn)的問題,下面這篇文章主要給大家總結(jié)介紹了關(guān)于JavaScript實現(xiàn)頁面跳轉(zhuǎn)的5種方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-12-12

