對於表單,input[type="radio"] 的樣式總是不那麼友好,在不同的瀏覽器中表現不一。
為了最大程度的顯示出它們的差別,並且為了好看,首先定義了一些樣式:
<span style="color: #000000;">html: </span><span style="color: #0000ff;"><</span><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">=""</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="sex"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="female"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="female"</span><span style="color: #0000ff;">></span>女<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="radio"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sex"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="female"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="male"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="male"</span><span style="color: #0000ff;">></span>男<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="radio"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sex"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="male"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #800000;">css: body </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0</span>; }<span style="color: #800000;"> input </span>{<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 0</span>; }<span style="color: #800000;"> .female, .male </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> line-height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> margin-left</span>:<span style="color: #0000ff;"> 40px</span>; }<span style="color: #800000;"> .sex label </span>{<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> line-height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> font-size</span>:<span style="color: #0000ff;"> 20px</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; }<span style="color: #800000;"> .sex input </span>{<span style="color: #ff0000;"> z-index</span>:<span style="color: #0000ff;"> 3</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> bottom</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> auto</span>;<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 30px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 30px</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; }
接著在各個瀏覽器中觀察,會發現有很大的差異:
ie:
edge:
opera:
chrome:
firefox:
對於 firefox 瀏覽器,即使設定了寬和高,還是沒有效果,input[type="radio"] 的那個圓圈還是初始狀態那麼大。其它瀏覽器的表現也不一致,為了達到一致的效果,我們需要做相容處理。
思路:
1. 將 input[type="radio"] 隱藏, opacity: 0; 置於上層,當我們點擊它時,就能正確的回應原本的事件。
2. 自訂一個圓圈,置於下層,模擬原本相似的樣式;
3. 用 js 實作選取 input[type="radio"] 時,在其下層的自訂的元素改變原來的背景顏色。
代碼:
<span style="color: #000000;">html: </span><span style="color: #0000ff;"><</span><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">=""</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="sex"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="female"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="female"</span><span style="color: #0000ff;">></span>女<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="radio"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sex"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="female"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="female-custom"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">> <!-- 同下面的 span 一样作为自定义的元素 --></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="male"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="male"</span><span style="color: #0000ff;">></span>男<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="radio"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="sex"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="male"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="male-custom"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #800000;">css: body </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0</span>; }<span style="color: #800000;"> input </span>{<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 0</span>; }<span style="color: #800000;"> .female, .male </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>; /* 设置为相对定位,以便让子元素能绝对定位 */<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> line-height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> margin-left</span>:<span style="color: #0000ff;"> 40px</span>; }<span style="color: #800000;"> .sex label </span>{<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> line-height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> font-size</span>:<span style="color: #0000ff;"> 20px</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; }<span style="color: #800000;"> .sex input </span>{<span style="color: #ff0000;"> z-index</span>:<span style="color: #0000ff;"> 3</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> bottom</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> auto</span>; /* 这里及以上的定位,可以让该元素竖直居中。(top: 0; bottom: 0;) */<span style="color: #ff0000;"> opacity</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 30px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 30px</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; }<span style="color: #800000;"> .sex span </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> bottom</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> auto</span>;<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 25px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 25px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid #000</span>;<span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; }<span style="color: #800000;"> .sex span.active </span>{<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> #000</span>; }
<span style="color: #000000;">js: $(</span>"#male").click( <span style="color: #0000ff;">function</span><span style="color: #000000;"> () { $(</span><span style="color: #0000ff;">this</span>).siblings("span").addClass("active"<span style="color: #000000;">); $(</span><span style="color: #0000ff;">this</span>).parents("div").siblings("div").children("span").removeClass("active"<span style="color: #000000;">); }); $(</span>"#female").click( <span style="color: #0000ff;">function</span><span style="color: #000000;"> () { $(</span><span style="color: #0000ff;">this</span>).siblings("span").addClass("active"<span style="color: #000000;">); $(</span><span style="color: #0000ff;">this</span>).parents("div").siblings("div").children("span").removeClass("active"<span style="color: #000000;">); });</span>
這樣處理後,在瀏覽器中展示效果全部一樣了:
擴充:
1. 對於程式碼中出現的定位,對父元素使用position: relative; 給子元素使用position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; 能實現讓子元素相對於父元素居中(滿足水平居中和垂直居中)顯示。如果只是需要垂直居中,則不需要加入 right: 0; 和 left: 0; 的樣式。
2. 有時當我們不容易確定子元素的高度時,可以這樣設定:對父元素position: relative; 對子元素position: absolute; top: 10px; bottom: 10px; margin: auto; 這樣一來,子元素的高度就是父元素的高度減去20px後的值了,同樣,top 和bottom 支援百分數,可擴展性更強。
最佳化更新:
需求:
1. 有時候我們需要內聯的單選樣式;
2. 選取的按鈕內的小圓圈不需要佔滿整個外圈的大小。
思路:
1. 讓每個包裹選取的 div 左浮動;
2. 為父元素新增圓形的外邊框,子元素設定一個稍小於父元素大小的背景。
代碼:
<span style="color: #000000;">html: </span><span style="color: #0000ff;"><</span><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">=""</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="fruit"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="apple"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="apple"</span><span style="color: #0000ff;">></span>苹果<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="radio"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="fruit"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="apple"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="user-defined"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="circle"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="banana"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="banana"</span><span style="color: #0000ff;">></span>香蕉<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="radio"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="fruit"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="banana"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="user-defined"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="circle"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="orange"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="orange"</span><span style="color: #0000ff;">></span>橘子<span style="color: #0000ff;"></</span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="radio"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="fruit"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="orange"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="user-defined"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="circle"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span>
<span style="color: #800000;">css: * </span>{<span style="color: #ff0000;"> box-sizing</span>:<span style="color: #0000ff;"> border-box</span>; }<span style="color: #800000;"> body </span>{<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 50px</span>; }<span style="color: #800000;"> input </span>{<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 0</span>; }<span style="color: #800000;"> .fruit:before </span>{<span style="color: #ff0000;"> content</span>:<span style="color: #0000ff;"> ""</span>;<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> table</span>; }<span style="color: #800000;"> .fruit:after </span>{<span style="color: #ff0000;"> content</span>:<span style="color: #0000ff;"> ""</span>;<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> table</span>;<span style="color: #ff0000;"> clear</span>:<span style="color: #0000ff;"> both</span>; }<span style="color: #800000;"> .fruit > div </span>{<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> relative</span>;<span style="color: #ff0000;"> float</span>:<span style="color: #0000ff;"> left</span>;<span style="color: #ff0000;"> margin-right</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 80px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> line-height</span>:<span style="color: #0000ff;"> 40px</span>; }<span style="color: #800000;"> .fruit > div:last-child </span>{<span style="color: #ff0000;"> margin-right</span>:<span style="color: #0000ff;"> 0</span>; }<span style="color: #800000;"> .fruit label </span>{<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> line-height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; }<span style="color: #800000;"> .fruit input </span>{<span style="color: #ff0000;"> z-index</span>:<span style="color: #0000ff;"> 3</span>;<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;"> opacity</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> bottom</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> auto</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 30px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 30px</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; }<span style="color: #800000;"> .fruit .user-defined </span>{<span style="color: #ff0000;"> z-index</span>:<span style="color: #0000ff;"> 2</span>;<span style="color: #ff0000;"> position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;"> top</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> bottom</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;"> left</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> auto</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 30px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 30px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid #000</span>;<span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;"> cursor</span>:<span style="color: #0000ff;"> pointer</span>; }<span style="color: #800000;"> .fruit .user-defined span.circle </span>{<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 24px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 24px</span>;<span style="color: #ff0000;"> margin-top</span>:<span style="color: #0000ff;"> 2px</span>;<span style="color: #ff0000;"> margin-left</span>:<span style="color: #0000ff;"> 2px</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> transparent</span>;<span style="color: #ff0000;"> border-radius</span>:<span style="color: #0000ff;"> 50%</span>; }<span style="color: #800000;"> .fruit .user-defined span.active </span>{<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> #000</span>; }
<span style="color: #000000;">js: $(</span>"input").click(<span style="color: #0000ff;">function</span><span style="color: #000000;">() { $(</span><span style="color: #0000ff;">this</span>).siblings("div").children("span").addClass("active"<span style="color: #000000;">); $(</span><span style="color: #0000ff;">this</span>).parents("div").siblings("div").find("span").removeClass("active"<span style="color: #000000;">); });</span>
效果顯示如下: