使用 JQuery 动态更改文本大小
Dynamically change Text size with JQuery
我有一个简单的索引.html包含 3 个分区,使用 JQuery,如何使用 <p> </p>
标签内的文本根据按钮放大或缩小 ZOOM+
和 ZOOM-
只有 3 个级别?
这是索引.html :
<html>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="jquery.js"></script>
<script>
$(document).ready(
function ()
{
var text = $('#d3').children('p').text();
$("#d1").click(function() {
$("#d2").show();
});
$("#btn1").click(function() {
});
});
</script>
</head>
<body>
<div id="d1">
En cliquant ici le slide va descendre ou remonter.
</div>
<div id="d2" hidden >
<div id="d3">
<p>HELLO</p>
</div>
<button type="button" id="btn1">ZOOM+</button>
<button type="button" id="btn2">ZOOM-</button>
</div>
</body>
</html>
风格.css :
#d1
{
width: 600px;
background-color: #e5eecc;
border-style: solid;
border: 1px 1px 1px 1px;
padding-left: 40px;
padding-right: 40px;
}
#d2
{
height: 160px;
width: 600px;
background-color: #e5eecc;
border-style: solid;
border: 1px 1px 1px 1px;
padding-left: 40px;
padding-right: 40px;
}
#d3
{
height: 100px;
widows: 200px;
background-color: #98bf21;
position: absolute;
}
#btn1
{
display: inline-block;
margin-left: 140px;
}
一个可能的解决方案可以是:
var initialSize = "16px";
$(function () {
var text = $('#d3').children('p').text();
var obj = $('p');
var initialSize = parseInt(obj.css('font-size')) + 'px';
$("#d1").click(function () {
$("#d2").show();
});
$("#btn1").click(function () {
var obj = $('p');
var fontSize = parseInt(obj.css('font-size')) + 1;
obj.css('font-size', fontSize + 'px');
});
$("#btn2").click(function () {
var obj = $('p');
var fontSize = parseInt(obj.css('font-size')) - 1;
obj.css('font-size', fontSize + 'px');
});
$("#btn3").click(function () {
$('p').css('font-size', initialSize);
});
});
#d1 {
width: 600px;
background-color: #e5eecc;
border-style: solid;
border: 1px 1px 1px 1px;
padding-left: 40px;
padding-right: 40px;
}
#d2 {
height: 160px;
width: 600px;
background-color: #e5eecc;
border-style: solid;
border: 1px 1px 1px 1px;
padding-left: 40px;
padding-right: 40px;
}
#d3 {
height: 100px;
widows: 200px;
background-color: #98bf21;
position: absolute;
}
#btn1 {
display: inline-block;
margin-left: 140px;
}
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>
<div id="d1">
En cliquant ici le slide va descendre ou remonter.
</div>
<div id="d2" hidden>
<div id="d3">
<p>HELLO</p>
</div>
<button type="button" id="btn1">ZOOM+</button>
<button type="button" id="btn2">ZOOM-</button>
<button type="button" id="btn3">ZOOM Reset</button>
</div>
使用 window.getComputedStyle
获取段落属性这是一个解决方案:
$(document).ready(
function ()
{
var paragraphElement = $('#d3').children('p');
console.log(paragraphElement.length);
$("#d1").click(function() {
$("#d2").show();
});
$("#btn1").click(function() {
for(var i=0;i<paragraphElement.length;i++){
var p= paragraphElement[i];
var style = window.getComputedStyle(p, null).getPropertyValue('font-size');
var fontSize = parseFloat(style);
p.style.fontSize = (fontSize + 1) + 'px';
};
});
$("#btn2").click(function() {
for(var i=0;i<paragraphElement.length;i++){
var p= paragraphElement[i];
var style = window.getComputedStyle(p, null).getPropertyValue('font-size');
var fontSize = parseFloat(style);
if(fontSize>2)
p.style.fontSize = (fontSize - 1) + 'px';
};
});
});
#d1
{
width: 600px;
background-color: #e5eecc;
border-style: solid;
border: 1px 1px 1px 1px;
padding-left: 40px;
padding-right: 40px;
}
#d2
{
height: 160px;
width: 600px;
background-color: #e5eecc;
border-style: solid;
border: 1px 1px 1px 1px;
padding-left: 40px;
padding-right: 40px;
}
#d3
{
height: 100px;
widows: 200px;
background-color: #98bf21;
position: absolute;
}
#btn1
{
display: inline-block;
margin-left: 140px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="d1">
En cliquant ici le slide va descendre ou remonter.
</div>
<div id="d2" hidden >
<div id="d3">
<p>HELLO</p>
<p> another parapgraph</p>
</div>
<button type="button" id="btn1">ZOOM+</button>
<button type="button" id="btn2">ZOOM-</button>
</div>
相关文章:
- 基于文本jquery php更改按钮
- 选择“p文本jquery”
- 附加 HTML 转义文本:jQuery
- 如何知道选中/突出显示的文本jquery的顺序出现
- Action.on('模糊')不't处理输入文本-jQuery 1.9+
- 在当前工具提示文本 JQuery 旁边追加或添加新文本
- 文本自动移动以适应动画相邻文本 (jquery):如何平滑
- 获取锚点的特定文本 - jQuery
- 从文本 jquery 中查找和挑选出模式
- 延迟后如何更改文本 - jQuery.
- 更改按钮文本 jQuery
- 使用 :包含 找到一个文本 jquery
- 获取引导程序单选按钮文本-jquery
- onchange函数转换为输入文本JQuery的数组
- 语法错误未终止字符串文本jquery
- 将输入占位符文本与span文本jquery交换
- 更改数值为文本- jQuery / NivoSlider
- 在文本区搜索指定的文本- jQuery/Javascript
- 一个字一个字地突出显示文本(jquery)(关闭)
- 无终止字符串文本jquery或更多