亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以下這三個HTML,jQuery,color是怎么計算的???求助大神!

以下這三個HTML,jQuery,color是怎么計算的啊?求助大神!

慕碼人2483693 2021-10-31 17:14:33
下面的例子將返回首個匹配元素的 background-color 值:<!DOCTYPE html><html><head><script src="/jquery/jquery-1.11.1.min.js"></script><script>$(document).ready(function(){$("button").click(function(){alert("Background color = " + $("p").css("background-color"));});});</script></head><body><h2>這是標題</h2><p style="background-color:#f5c000">這是一個段落。</p><p style="background-color:#00ff00">這是一個段落。</p><p style="background-color:#0000ff">這是一個段落。</p><p style="background-color:#0f2345">這是一個段落。</p><p style="background-color:#c04a1f">這是一個段落。</p><button>返回 p 元素的背景色</button></body></html>這里怎么計算的啊 元素的 background-color 值alert("Background color = " + $("p").css("background-color"));
查看完整描述

2 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

background-color 是 string
是無法計算的
獲取 background-color
var bgc = $('p').eq(0).css('backgroundColor');
alert( "Background color = " + bgc);
這里的eq(0)是指的第一個p元素

在css()方法中,如果屬性中帶有“-”符號,例如font-size和background-color屬性,如果在設置這些屬性的值的時候不帶引號,那么就要用駝峰式寫法,比如上面的代碼,如果帶上了引號,既可以寫成“'background-color'”,也可以寫成“'backgroundColor'”。



查看完整回答
反對 回復 2021-11-06
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

$.fn.getBackgroundColor = function() {     var rgb = $(this).css('background-color');     if(rgb >= 0) return rgb;//如果是一個hex值則直接返回    else{         rgb = rgb.match(/^rgb(\d+),\s?(\d+),\s?(\d+)$/);         function hex(x) {return ("0" + parseInt(x).toString(16)).slice(-2);}         rgb= "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);     }     return rgb; }

可以用這個對象函數獲取,因為瀏覽器不兼容,所以返回的屬性值是不同的,這個函數可以解決


查看完整回答
反對 回復 2021-11-06
  • 2 回答
  • 0 關注
  • 296 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號