2012-11-06 48 views
1

我有一條線,我添加一些CSS:添加a.link將jQuery的.css

$("#foo").html("Some text with a link <a href=\"link.html\">Link here</a>").css({ 
    'margin': '10px 0px', 
    'padding': '15px 10px 15px 50px', 
    'clear': 'left' 
}); 

我想改變的鏈接顏色發黑,所以加入這個CSS應該工作:

a.link { 
    color: black 
} 

我想要做的最好的事情是指向一個css文件,而不是添加a.link。但想知道是否有可能。顯然,這樣的事情並沒有工作,因爲它不是正確的JSON格式:

$("#foo").html("Some text with a link <a href=\"link.html\">Link here</a>").css({ 
    'margin': '10px 0px', 
    'padding': '15px 10px 15px 50px', 
    'clear': 'left', 
    'a:link: color': 'black' 
}); 

有沒有辦法做到這一點?

+1

_As是不正確的JSON文件格式有?您傳遞的不是JSON的常規JavaScript對象。 – undefined

回答

0

爲什麼不插入樣式標籤與所需樣式? <style type="text/css">a.link: {color: black}</style>或定義鏈接顏色直列

$("#foo").html('Some text with a link <a style="color: black" href="link.html">Link here</a>').css({'margin': '10px 0px', 'padding': '15px 10px 15px 50px', 'clear': 'left'}); 

.css由選擇用於定義內聯的CSS樣式,不樣式,像a.link,用於該<style>標籤。

7

您無法從父元素css函數更新子元素的樣式。相反,爲什麼不將它添加到CSS文件,

#foo a.link { color: black; } 

在情況下,如果你是如何通過腳本來添加它好奇..

$("#foo")   //Added class link to the link tag--v 
    .html('Some text with a link <a href=\"link.html\" class="link">Link here</a>') 
    .css({'margin': '10px 0px', 'padding': '15px 10px 15px 50px', 'clear': 'left'}) 
    .find('a.link').css({'color': 'black'}); 
    //^-- this would find the link tag and updates its css