[英]Not set css attributes to child elements. Jquery
我的div
里面有一张table
。 在表格的td
元素中,我有文本和一些具有'actionLink'类分配的链接。
使用JQuery,我将一些属性值更改为父div
内的td
和p
元素。 一切正常,但是链接的属性值也会更改。
有没有一种方法可以更改此属性而不影响a
元素。
这就是我所拥有的
的JavaScript
$('.sectionContent').css("color", $('#ParagraphForegroundColor').val());
$('.sectionContent').css("background-color", $('#ParagraphBackgroundColor').val());
$('.sectionContent p, .sectionContent td').not(".addColumn").css("font-family", $('#ParagraphFontFamily').val());
$('.sectionContent p, .sectionContent td').not(".addColumn").css("font-size", $('#ParagraphFontSize').val());
$('.sectionContent p, .sectionContent td').not(".addColumn").css("font-style", $('#ParagraphFontStyle').val());
$('.sectionContent p, .sectionContent td').not(".addColumn").css("font-weight", $('#ParagraphFontWeight').val());
$('.sectionContent p, .sectionContent td').not(".addColumn").css("text-decoration", $('#ParagraphTextDecoration').val());
更新 :在您的选择更仔细的你已经证明不符合任何选择a
水平,除非他们有类“sectionContent”元素应用到所有的元素,在这种情况下,他们通过你的第一个两行相匹配与“ sectionContent”类。 但其余五线显然只选择p
和td
元素,所以不会有任何(直接)影响a
在所有元素。
当然,它们可能会产生间接影响。 如果a
元素从其所在的p
或td
继承其字体设置(并且经常这样做),那么当然更改p
或td
的字体设置将间接影响a
因为它继承了它们。 除了在a
元素上显式设置字体设置之外,其他方法无济于事。
例如:
CSS:
p {
font-family: serif;
}
a.actionLink {
font-family: sans-serif;
}
HTML:
<p>This is the paragraph with <a href="#" class="actionLink">an 'actionLink' link in it</a>.
JavaScript:
$("p").css("font-weight", "bold");
现在,该链接为粗体,因为它是从其父元素继承其font-weight
。 相反:
$("p").css("font-family", "Comic Sans"); // Hey, I had to do it
这对a
元素没有影响,因为它具有自己的 font-family
设置。
原始答案 :
很难明确地回答而不看到您的标记,但是通常:您可以将选择器设置为仅匹配具有类且具有特定标签名称的元素,例如:
// Matches *all* elements with the class "className"
$(".className")
// Only matches `p` elements with "className"
$("p.className")
// Only matches `td` and `p` elements with "className"
$("p.className, td.className")
jQuery支持几乎所有的CSS3选择器,还有一些。 细节:
实际上,我认为您只需要在css中定义class actionLink,这样它就不会继承包含它的TD的属性。
就像是
.actionLink{
font-family:vlaue;
font-size:vlaue;
font-weight:vlaue;
font-style:vlaue;
text-decoration:vlaue;
}
编辑:
对于实例,这可以正常工作:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
<script type="text/javascript">
$(function(){
$('.sectionContent').css("color", 'blue');
$('.sectionContent').css("background-color", 'red');
$('.sectionContent p, .sectionContent td').not(".addColumn").css("font-family", '"Times New Roman",Georgia,Serif');
$('.sectionContent p, .sectionContent td').not(".addColumn").css("font-size", '11pt');
$('.sectionContent p, .sectionContent td').not(".addColumn").css("font-style", 'normal');
$('.sectionContent p, .sectionContent td').not(".addColumn").css("font-weight", 'normal');
$('.sectionContent p, .sectionContent td').not(".addColumn").css("text-decoration", 'none');
});
</script>
<style>
.actionLink{
font-family:Arial;
font-size:6pt;
font-weight:bold;
font-style:normal;
text-decoration:underline;
background-color:white;
color:black;
}
</style>
</head>
<body>
<div class="sectionContent">
<table>
<tr>
<td>
Some Test
<a class='actionLink'>Action</a>
</td>
</tr>
</table>
<p>Some Content</p>
</div>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.