這篇“jquery如何改變style樣式”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“jquery如何改變style樣式”文章吧。
創(chuàng)新互聯(lián)網(wǎng)站建設(shè)提供從項(xiàng)目策劃、軟件開發(fā),軟件安全維護(hù)、網(wǎng)站優(yōu)化(SEO)、網(wǎng)站分析、效果評估等整套的建站服務(wù),主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計,APP應(yīng)用開發(fā)以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。創(chuàng)新互聯(lián)深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
jquery改變style樣式的方法:1、利用css()給指定元素設(shè)置新style樣式即可,語法“$(selector).css({"屬性名1":"屬性值1","屬性名2":"屬性值2",...})”;2、使用attr()設(shè)置指定元素的style屬性值即可,語法“$(selector).attr("style","屬性名1:屬性值1;屬性名2:屬性值3;...")”。
jquery改變style樣式有兩種方法:
通過css()方法來修改
通過attr()方法來修改
方法1:使用css()方法
css() 方法可以設(shè)置匹配的元素的一個或多個樣式屬性。
直接利用css() 方法設(shè)置新style樣式即可
語法:
$(selector).css({"屬性名1":"屬性值1","屬性名2":"屬性值2",...})
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="js/jquery-3.6.3.min.js"></script>
<script>
$(document).ready(function() {
$("button").on("click", function() {
$("div").css({"color":"red","background-color":"papayawhip","font-size":"25px"});
});
});
</script>
<style>
.tr1 {
color: green;
}
.tr2 {
color: red;
background-color: blanchedalmond;
}
</style>
</head>
<body class="ancestors">
<div style="color: yellow;background-color:powderblue;">歡迎來到創(chuàng)新互聯(lián)!</div><br>
<button>改變div元素的style樣式</button>
</body>
</html>
方法2:使用attr()方法
attr() 方法設(shè)置或返回被選元素的屬性值。
只需要使用attr() 方法設(shè)置元素的style屬性值即可。
語法:
$(selector).attr("style","屬性名1:屬性值1;屬性名2:屬性值3;...")
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="js/jquery-3.6.3.min.js"></script>
<script>
$(document).ready(function() {
$("button").on("click", function() {
$("div").attr("style", "color:pink;background-color:peru;font-size: 18px;");
});
});
</script>
<style>
.tr1 {
color: green;
}
.tr2 {
color: red;
background-color: blanchedalmond;
}
</style>
</head>
<body class="ancestors">
<div style="color: red;background-color:papayawhip;">歡迎來到創(chuàng)新互聯(lián)!</div><br>
<button>改變div元素的style樣式</button>
</body>
</html>
以上就是關(guān)于“jquery如何改變style樣式”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:jquery如何改變style樣式
標(biāo)題鏈接:http://aaarwkj.com/article38/gjocsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、營銷型網(wǎng)站建設(shè)、定制開發(fā)、建站公司、網(wǎng)站改版、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)