JQuery功能,可在任何地方增加字体大小
JQuery function to increase the font size everywhere
我正在尝试创建一个单击函数来增加每段文本(p
,a
,span
,h1
等)的字体大小。我不想使用这样的选择器
var allText = $('.page-container a, .page-container p, .page-container h1, .page-container h2, ...');
因为我希望这是通用的和可维护的。我的愿望能实现吗?
给定简化的 HTML,如下所示(因为您在问题中没有提供任何内容):
<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>
和 CSS:
/* 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;
}
以下 jQuery 将起作用:
// 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';
});
});
JS小提琴演示。
引用:
-
css()
. -
on()
.
$('*').css('font-size', '15px');
可能会做这个伎俩。
此解决方案按比例放大文本。
$(document).ready(function() {
$('.big').on('click', function() {
$('*').each(function() {
var fontsize = parseInt($(this).css('font-size'));
console.log(fontsize);
var newFontsize = (fontsize * 1.1) + 'px';
console.log(newFontsize)
$(this).css('font-size', newFontsize);
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="big">englarge<a/>
<p>askdjfhasjdhf</p>
<a>lkajsdlfkjasdf</a>
<h1>askjdflasf</h1>
a;sldkfalskdfsdf
相关文章:
- 根据元素高度和宽度的百分比变化增加或减少字体大小
- 使用jquery在按钮点击事件中增加整个网页的字体大小
- 增加了“上传”按钮的文本字体大小
- JQuery功能,可在任何地方增加字体大小
- 增加 em 的字体大小
- 如何增加安卓应用程序的jqMath公式的字体大小
- 如何增加推特提要小部件的宽度和字体大小
- 如何让用户通过单击文本来增加字体大小
- 增加/减少页面上所有元素字体大小的有效方法
- 增加选项卡式导航中文本的字体大小
- 按比例增加整个网站的每个字体大小
- 如何通过快捷方式增加和减少字体大小?(tinymce)
- 通过javascript在每次点击时动态增加字体大小
- 无法在单击按钮时增加字体大小
- 我如何动态地增加字体大小与Javascript和为什么是我目前的功能不工作
- 如何使用jquery增加字体大小?
- 标签云——根据编号增加字体的权重
- Css保持表格宽度保持不变,而用户按ctrl +在chrome上增加字体大小
- 只需使用JavaScript点击按钮即可增加字体大小
- Jquery自动完成聚焦项增加字体大小