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

來源:互聯網
上載者:User

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

一些簡單的例子,用原生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>

jquery 或者原生js 擷取 style 裡的某一屬性值

document.getElementById(id).style.FILTER

在元語句中加個id
 
jquery div屬性為display:none js取值問題

如果你要取div裡面的值 跟是否隱藏無關 $("#div").text() 你用這個試試~~ attr('value')等同於.val()

只針對有value屬性的控制項有用
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.