Javascript JQuery函数可以在任何地方增加字体大小
我正在尝试创建一个单击函数来增加每段文本的字体大小(Javascript JQuery函数可以在任何地方增加字体大小,javascript,jquery,Javascript,Jquery,我正在尝试创建一个单击函数来增加每段文本的字体大小(p,a,span,h1,等等)。我不想使用像这样的选择器 var allText = $('.page-container a, .page-container p, .page-container h1, .page-container h2, ...'); 因为我希望它是通用的和可维护的。我的愿望能实现吗 $('*').css('font-size', '15px'); 可能会有效果。此解决方案按比例放大文本 $(文档).ready(
p
,a
,span
,h1
,等等)。我不想使用像这样的选择器
var allText = $('.page-container a, .page-container p, .page-container h1, .page-container h2, ...');
因为我希望它是通用的和可维护的。我的愿望能实现吗
$('*').css('font-size', '15px');
可能会有效果。此解决方案按比例放大文本
$(文档).ready(函数(){
$('.big')。在('click',function()上{
$('*')。每个(函数(){
var fontsize=parseInt($(this.css('font-size'));
控制台日志(fontsize);
变量newFontsize=(fontsize*1.1)+‘px’;
console.log(newFontsize)
$(this.css('font-size',newFontsize);
});
});
});代码>
askjdflasf
A.sldkfalskdfsdf
给出了简化的HTML,如以下内容(因为您没有提供任何问题):
+-
标题文本
潜水艇
- 各种元素
还有一个标题
参考资料:
如果您的子元素的字体大小以相对单位(%
,em
,等等)声明,那么您可以通过增加
元素的字体大小来实现这一点,而其他元素应该以该元素为基础。如果您在整个应用程序中使用相对字体大小(例如em),是的,这很容易。您只需要增加根元素(通常在主体中)的字体大小,其他所有内容都将相应地进行调整。$('body').css('font-size','DesiredNewsIzeInPixelSorrcentageRowhateVerlikeem')代码>?如果您真的想将其应用于每一个文本,那么为什么不直接从文档中应用它呢?使用基本字体大小作为正文,em else where是最安全的方法。这可以设置所有元素的大小,但会破坏元素之间字体大小的比例/比率。可能;如果他在标题(
,
…)元素中使用了非常小的字体大小。在某些情况下,正如David所建议的,这将减少字体大小,而不是增加字体大小。现在,这是一个解决方案,干得好,当之无愧的+1!另外,非常好地使用了.css回调。非常感谢您!)您不需要each()
,只需使用css()
:$('*').css('font-size',function(i,currentSize){return(parseInt(currentSize,10)+2)+'px';})代码>例如;不管怎样,jQuery都会在内部迭代所有内容,您也可以使用它。
<button id="up">+</button><button id="down">-</button>
<h1>Heading text</h1>
<div>
<p>A div, with</p>
<ul>
<li>Various elements</li>
</ul>
<h2>And another heading</h2>
<aside>At this point, I'm sure that you get the point</aside>
<blockquote>But if you don't, then I'm sure it can be explained</blockquote>
</div>
/* This is more or less irrelevant, it's just to show
that the <body> element has an explicit font-size,
in pixels (but em would work also) */
body {
font-size: 16px;
}
/* and all other elements have relative font-sizes,
basing their own font-size upon the inherited
font-size from their ancestors: */
h1 {
font-size: 2em;
}
div {
font-size: 1em;
}
ul {
font-size: 1.2em;
}
h2 {
font-size: 1.5em;
}
aside {
font-size: 0.8em;
}
blockquote {
float: left;
font-size: 1.2em;
border-left: 2px solid #f90;
padding: 0.5em;
}
// binds a simple click event-handler:
$('button').on('click', function(){
// caching the clicked element:
var clicked = this;
// selecting the <body> element,
// using the css() method to set the 'font-size'
// via the anonymous function:
$('body').css('font-size', function (i,size) {
// i: the index of the current element in the collection
// size: the current value of the current element
// using parseInt to get a number in base-ten,
// adding either 2 (if the clicked element was #up)
// or -2 (if the clicked element was not #up):
// appending 'px' to the number to make it a valid
// css unit:
return parseInt(size,10) + (clicked.id === 'up' ? 2 : -2) + 'px';
});
});