【发布时间】:2015-11-19 06:25:32
【问题描述】:
我有一个在其他地方生成的列表,我无法更改它。 缩进级别是通过 ,但是每个缩进的字体大小是相同的,我想根据缩进级别更改字体大小。 因此,我需要复制一个 css 规则并更改新的 id 和 font-size。
以下是 HTM 生成的代码,我无法更改:
<style type="text/css">
span.text12Font1 {
font-size:14px;
font-family:"Arial", sans-serif;
color:#010101;
font-weight:normal;
font-style:normal;
text-decoration:normal;
}
</style>
<div id="text12">
</a>
<ul style="margin-left:4px;text-align:left;" >
<li>
<span class="text12Font1">Emphasize the beginning of the bullet point</span>
</li>
<li>
<span class="text12Font1"> </span >
<span class="text12Font1">As in this list, when the first few words capture the main idea</span >
</li>
<li>
<span class="text12Font1"> That way, readers can skim easily</span>
</li>
</ul>
</div>
我可以得到每个点,并且可以找到每个点中的所有类名。 我需要的是能够复制一个 css 类,给它一个新的 id 并改变字体大小。
到目前为止,我有以下内容:
function getNewClassName(className, newName, fSize){
var spanID = 'span.' + className;
//e.g.: span.text12Font1
for(var i=0; i<document.styleSheets.length; i++){
var sheet = document.styleSheets[i];
var cssText = sheet.ownerNode.innerText;
var posn = cssText.indexOf(spanID);
if(posn!=-1){
var oSheet = document.styleSheets[i];
var oRules = oSheet.cssRules ? oSheet.cssRules : oSheet.rules;
for(var r=0; r<oRules.length; r++){
if(oRules[r].cssText.indexOf(spanID)!=-1){
// Here I have the rule that I want to duplicate, change it's name to newName and change the font-size.
// I must not change the existing rule and it must remain as it could be used elsewhere
return true;
}
}
}
}
return false;
}
【问题讨论】:
-
屏幕阅读器无法捕捉通过这种方式表达的层次结构。在需要层次结构的地方应使用嵌套列表。
-
为什么需要复制 CSS 类?为什么不使用相同的 CSS 类而只覆盖字体大小?
-
你确定你不能修改生成代码的代码吗...因为那是 100% 简单的
-
我无法更改 HTML 代码,我无法更改现有样式,我需要使用现有样式创建新样式并应用它。这是我专门问的。我只能使用 JavaScript,因为 jQuery 没有空间,这不是我的要求,它就是这样。
-
顺便说一句,这实际上比上面要复杂得多,我只需要能够在现有样式的基础上创建一个新样式,然后更改我需要的内容。一旦给出一个示例,我将能够将其带到下一个更复杂的步骤。 TIA
标签: javascript css