標籤:ber ring orm dex ntb 開頭 結合 ott tom
轉載自:http://www.jb51.net/article/81984.htm
一. 緣由:
本文源於在OSC社區中,有人提問如何用jq擷取虛擬元素。我第一想法是強大的CSS Query應該可以擷取虛擬元素吧。
然而事實上,CSS Query並不能。即我們不能通過$(“:before”)、$(dom).find(“:before”)或document.querySelector(“:before”)來擷取:before虛擬元素。
為此,我不得不重新瞭解虛擬元素(Pseudo-elements)。為什麼不能用JS直接擷取虛擬元素呢?
譬如::before和::after虛擬元素,用於在CSS渲染中向元素的頭部或尾部插入內容,它們不受文檔約束,也不影響文檔本身,隻影響最終樣式。這些添加的內容不會出現在DOM中,僅僅是在CSS渲染層中加入。
事實上, 虛擬元素可以被瀏覽器渲染,但本身並不是DOM元素。它不存在於文檔中,所以JS無法直接操作它。 而jQuery的選取器都是基於DOM元素的,因此也並不能直接操作虛擬元素。
那該怎樣操作虛擬元素的樣式呢?
為此,我決定好好總結一下JS控制虛擬元素的方法,方便以後查用。
二. 虛擬元素有哪些:
首先,先簡單說一下虛擬元素都有哪些。虛擬元素有六個,分別是 ::after、::before、::first-line、::first-letter、::selection、::backdrop 。
在各大網頁中最常用的虛擬元素,是::after和::before。
這幾個虛擬元素的語意可參考我的另外一篇文章《CSS虛擬元素選取器 總結》。
在CSS3中,建議虛擬元素使用兩個冒號(::)文法,而不是一個冒號 (:),目的是為了區分偽類和虛擬元素。大多數瀏覽器都支援這兩種表示文法。只有 ::selection 永遠只能以兩個冒號開頭(::)。因為IE8隻支援單冒號的文法,所以,如果你想相容IE8,保險的做法是使用單冒號。
三. 擷取虛擬元素的屬性值:
擷取虛擬元素的屬性值可以使用 window.getComputedStyle() 方法,擷取虛擬元素的CSS樣式聲明對象。然後利用getPropertyValue方法或直接使用索引值訪問都可以擷取對應的屬性值。
文法:window.getComputedStyle(element[, pseudoElement])
參數如下:
element(Object):虛擬元素的所在的DOM元素;
pseudoElement(String):虛擬元素類型。可選值有:”:after”、”:before”、”:first-line”、”:first-letter”、”:selection”、”:backdrop”;
舉個栗子:
?
| 1234567891011121314151617 |
// CSS代碼#myId:before {content: "hello world!";display: block;width: 100px;height: 100px;background: red;}// HTML代碼<div id="myId"></div>// JS代碼var myIdElement = document.getElementById("myId");var beforeStyle = window.getComputedStyle(myIdElement, ":before");console.log(beforeStyle); // [CSSStyleDeclaration Object]console.log(beforeStyle.width); // 100pxconsole.log(beforeStyle.getPropertyValue("width")); // 100pxconsole.log(beforeStyle.content); // "hello world!" |
備忘:
1.getPropertyValue()和直接使用索引值訪問,都可以訪問CSSStyleDeclaration Object。它們兩者的區別有:
對於float屬性,如果使用索引值訪問,則不能直接使用getComputedStyle(element, null).float,而應該是cssFloat與styleFloat;
直接使用索引值訪問,則屬性的鍵需要使用駝峰寫法,如:style.backgroundColor;
使用getPropertyValue()方法不必可以駝峰書寫形式(不支援駝峰寫法),例如:style.getPropertyValue(“border-top-color”);
getPropertyValue()方法在IE9+和其他現代瀏覽器中都支援;在IE6~8中,可以使用getAttribute()方法來代替;
2.虛擬元素預設是”display: inline”。如果沒有定義display屬性,即使在CSS中顯式設定了width的屬性值為固定的大小如”100px”,但是最後擷取的width值仍是”auto”。這是因為行內元素不能自訂設定寬高。解決辦法是給虛擬元素修改display屬性為”block”、”inline-block”或其他。
四. 更改虛擬元素的樣式:
方法1. 更換class來實現虛擬元素屬性值的更改:
舉個栗子:
?
| 12345678910111213 |
// CSS代碼.red::before { content: "red"; color: red; }.green::before { content: "green"; color: green;}// HTML代碼<div class="red">內容內容內容內容</div>// jQuery代碼$(".red").removeClass(‘red‘).addClass(‘green‘); |
方法2. 使用CSSStyleSheet的insertRule來為虛擬元素修改樣式:
舉個栗子:
?
| 1 |
document.styleSheets[0].addRule(‘.red::before‘,‘color: green‘); // 支援IE document.styleSheets[0].insertRule(‘.red::before { color: green }‘, 0); // 支援非IE的現代瀏覽器 |
方法3. 在 <head> 標籤中插入 <style> 的內部樣式:
?
| 12345 |
var style = document.createElement("style"); document.head.appendChild(style); sheet = style.sheet; sheet.addRule(‘.red::before‘,‘color: green‘); // 相容IE瀏覽器sheet.insertRule(‘.red::before { color: green }‘, 0); // 支援非IE的現代瀏覽器 |
或者用jQuery:
?
| 1 |
$(‘<style>.red::before{color:green}</style>‘).appendTo(‘head‘); |
五. 修改虛擬元素的content的屬性值:
方法1. 使用CSSStyleSheet的insertRule來為虛擬元素修改樣式:
?
| 12 |
var latestContent = "修改過的內容";var formerContent = window.getComputedStyle($(‘.red‘), ‘::before‘).getPropertyValue(‘content‘); document.styleSheets[0].addRule(‘.red::before‘,‘content: "‘ + latestContent + ‘"‘); document.styleSheets[0].insertRule(‘.red::before { content: "‘ + latestContent + ‘" }‘, 0); |
方法2. 使用DOM元素的data-*屬性來更改content的值:
?
| 123456789 |
// CSS代碼.red::before {content: attr(data-attr);color: red;}// HTML代碼<div class="red" data-attr="red">內容內容內容內容</div>// JacaScript代碼$(‘.red‘).attr(‘data-attr‘, ‘green‘); |
六. :before和:after虛擬元素的常見用法總結:
1. 利用content屬性,為元素新增內容修飾:
1) 添加字串:
使用引號包括一段字串,將會向元素內容中添加字串。栗子:
?
| 1 |
a:after { content: "after content"; } |
2) 使用attr()方法,調用當前元素的屬性的值:
栗子:
?
| 12 |
a:after { content: attr(href); }a:after { content: attr(data-attr); } |
3)使用url()方法,引用多媒體檔案:
栗子:
?
| 1 |
a::before { content: url(logo.png); } |
4) 使用counter()方法,調用計時器:
栗子:
?
| 1 |
h:before { counter-increment: chapter; cotent: "Chapter " counter(chapter) ". " } |
2. 清除浮動:
?
| 12 |
.clear-fix { *overflow: hidden; *zoom: 1; }.clear-fix:after { display: table; content: ""; width: 0; clear: both; } |
3. 特效妙用:
?
| 1234567891011121314151617181920212223242526 |
// CSS代碼a {position: relative;display: inline-block;text-decoration: none;color: #000;font-size: 32px;padding: 5px 10px;}a::before, a::after { content: "";transition: all 0.2s;}a::before { left: 0;}a::after { right: 0;}a:hover::before, a:hover::after { position: absolute;}a:hover::before { content: "\5B"; left: -20px; }a:hover::after { content: "\5D"; right: -20px; }// HTML代碼<a href="#">我是個超連結</a> |
4. 特殊形狀的實現:
舉個栗子:(譬如對話氣泡)
?
| 12345678910111213141516171819202122 |
// CSS代碼.tooltip {position: relative;display: inline-block;padding: 5px 10px;background: #80D4C8;}.tooltip:before {content: "";display: block;position: absolute;left: 50%;margin-left: -5px;bottom: -5px;width: 0; height: 0; border-left: 5px solid transparent;border-right: 5px solid transparent;border-top: 5px solid #80D4C8;}// HTML代碼<div class="tooltip">I‘m a tooltip.</div> |
:before 和 :after 虛擬元素結合更多CSS3強大的特性,還可完成非常多有趣的特效和 hack ,這裡權當拋磚引玉。
六. 一點小小建議:
虛擬元素的content屬性很強大,可以寫入各種字串和部分多媒體檔案。但是虛擬元素的內容只存在於CSS渲染樹中,並不存在於真實的DOM中。所以為了SEO最佳化,最好不要在虛擬元素中包含與文檔相關的內容。
修改虛擬元素的樣式,建議使用通過更換class來修改樣式的方法。因為其他兩種通過插入行內CSSStyleSheet的方式是在JavaScript中插入字元代碼,不利於樣式與控制分離;而且字串拼接易出錯。
修改虛擬元素的content屬性的值,建議使用利用DOM的data-*屬性來更改。
以上所述是小編給大家介紹的JS控制虛擬元素的方法匯總,希望對大家有所協助!
JS控制虛擬元素的方法匯總