900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > java div style_用js实现改变随意改变div属性style的名称和值的结果

java div style_用js实现改变随意改变div属性style的名称和值的结果

时间:2021-11-05 13:28:45

相关推荐

java div style_用js实现改变随意改变div属性style的名称和值的结果

一些简单的例子,用原生js和jQuery实现随意改变div属性,和重置。代码如下:

函数传参,改变Div任意属性的值

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;}

函数传参,改变Div任意属性的值

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;}

/*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");

})

})

属性名:

属性值:

确定重置

在上方输入框输入"属性名"及"属性值",点击确定按钮查看效果。

$(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");

})

})

属性名:

属性值:

确定重置

在上方输入框输入"属性名"及"属性值",点击确定按钮查看效果。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。