简体   繁体   English

如何增加整个标签的字体大小

[英]How do I increase font size of an entire tag

I'm running into an issue with an assignment.我遇到了一个任务问题。 Part of the assignment is to increase the font size of a paragraph using jQuery or JavaScript.部分任务是使用 jQuery 或 JavaScript 增加段落的字体大小。

The console statements seem to increase +1 every mouse click (the first click counts for 1, the second for two, etc).每次单击鼠标时,控制台语句似乎都会增加 +1(第一次单击计为 1,第二次计为 2,以此类推)。

Thanks.谢谢。

 function fontUp() { var fSize = document.getElementsByTagName('p'); $('#font-up').click(function() { console.log('font-up clicked'); fSize++; }); } function fontDn() { var fSize = document.getElementsByTagName('p'); $('#font-dn').click(function() { console.log('font-down clicked'); fSize--; }); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="content"> <p id='p1'>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Qui iste deserunt voluptate unde, pariatur aliquam consequatur, aut neque accusamus consequuntur, odit velit exercitationem non eligendi fuga repudiandae! Omnis, veritatis officiis.</p> <br> <p id='p2'>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Qui iste deserunt voluptate unde, pariatur aliquam consequatur, aut neque accusamus consequuntur, odit velit exercitationem non eligendi fuga repudiandae! Omnis, veritatis officiis.</p> </div> <div id="selectors"> <button id="button1" class="color-button" onclick="changeColorRed()">Red</button> <button id="button2" class="color-button" onclick="changeColorBlue()">Blue</button> <button id="button3" class="color-button" onclick="changeColorGreen()">Green</button> <br> <br> <input id="red-input" class="input-box" type="text" placeholder="red"></input> <input id="green-input" class="input-box" type="text" placeholder="green"></input> <input id="blue-input" class="input-box" type="text" placeholder="blue"></input> <button id="rgb-button" class="color-button" onclick="changeRGB()">Change Background</button> <br> <br> <button id="font-up" onclick="fontUp()">Font Up</button> <button id="font-dn" onclick="fontDn()">Font Down</button> </div> </div>

First, inputs are self-closing, so you don't need the </input> at the end.首先,输入是自动关闭的,所以最后不需要</input>

Outside that, as pointed out in comments, you're just grabbing all p elements, and trying to perform an increment or decrement operator on the array of elements, which won't do anything.除此之外,正如评论中指出的那样,您只是获取所有p元素,并尝试p元素数组执行递增或递减运算符,这不会做任何事情。 You'd want to get the current font size as a number (you can either use replace(/[^0-9]/g, '') on the font size or just parseInt() to remove the px , then increment/decrement, then assign it back to the paragraphs.您希望将当前字体大小作为数字(您可以在字体大小上使用replace(/[^0-9]/g, '')或仅使用parseInt()删除px ,然后增加/递减,然后将其分配回段落。

function fontUp(){
  //get the current font-size as an integer
  var fSize = parseInt($('p').css('font-size'));
  //use a pre-increment operator to add one to the font size and assign it to the paragraphs at the same time
  $('p').css('font-size', ++fSize);
}

function fontDn(){
  var fSize = parseInt($('p').css('font-size'));
  $('p').css('font-size', --fSize);
}

https://jsfiddle.net/n2ydp458/ https://jsfiddle.net/n2ydp458/

Edit: I answered a bit too quick and left your event listeners that were being mapped inside event listeners in there.编辑:我回答的有点太快了,把你的事件侦听器留在那里的事件侦听器中。 I've removed them now.我现在已经删除了它们。

Consider the following.考虑以下。

 $(function() { var fonts = [ "font-sm", "font-md", "font-rg", "font-lg", "font-xl" ]; var fSize = 2; function findFont(i, c) { if (c.indexOf("font") === 0) { return c; } } function changeFont(dir) { if (dir == "up") { fSize++; } else { fSize--; } if (fSize < 0) { fSize = 0; } if (fSize >= fonts.length - 1) { fSize = fonts.length - 1; } $("#content p").removeClass(findFont).addClass(fonts[fSize]); } function changeColor(cName) { $("#content p").removeClass("red blue green").addClass(cName); } $("button[id^='font']").click(function() { if ($(this).is("#font-up")) { changeFont("up"); } else { changeFont("down"); } console.log("Font Change", fonts[fSize]); }); $(".color-button").click(function() { changeColor($(this).val()); console.log("Color Changed", $(this).val()); }); });
 .font-1, .font-sm { font-size: 11px; } .font-2, .font-md { font-size: 12px; } .font-3, .font-rg { font-size: 14px; } .font-4, .font-lg { font-size: 18px; } .font-5, .font-xl { font-size: 24px; } .red { color: red; } .blue { color: blue; } .green { color: green; } #selectors input { display: block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="content"> <p id='p1' class="font-rg">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Qui iste deserunt voluptate unde, pariatur aliquam consequatur, aut neque accusamus consequuntur, odit velit exercitationem non eligendi fuga repudiandae! Omnis, veritatis officiis.</p> <p id='p2' class="font-rg">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Qui iste deserunt voluptate unde, pariatur aliquam consequatur, aut neque accusamus consequuntur, odit velit exercitationem non eligendi fuga repudiandae! Omnis, veritatis officiis.</p> </div> <div id="selectors"> <button id="button1" class="color-button" value="red">Red</button> <button id="button2" class="color-button" value="blue">Blue</button> <button id="button3" class="color-button" value="green">Green</button> <input id="red-input" class="input-box" type="text" placeholder="red" /> <input id="green-input" class="input-box" type="text" placeholder="green" /> <input id="blue-input" class="input-box" type="text" placeholder="blue" /> <button id="rgb-button" class="color-button" onclick="changeRGB()">Change Background</button> <button id="font-up">Font Up</button> <button id="font-dn">Font Down</button> </div> </div>

This lets you control the Font Sizes via CSS.这让您可以通过 CSS 控制字体大小。 It also ensure that the User cannot decrease or increase the font size too much.它还确保用户不能过多地减少或增加字体大小。 You can define your own Class names or how many you want.您可以定义自己的类名或您想要的类名。 You also get to define the sizes as you want as well.您还可以根据需要定义尺寸。

See more:查看更多:

I think you have to adjust your fontUp and fontDn function.我认为您必须调整您的fontUpfontDn功能。 It's right to get the elements with p tag, but then one of the way to achieve that is to loop all those elements, and then get each of their current font-size , and increment it by 1 on fontUp and decrement it by 1 on fontDown获取带有p标签的元素是正确的,但是实现这一点的方法之一是循环所有这些元素,然后获取它们当前的每个font-size ,并在fontUp其递增 1 并在 on fontUp其递减 1 fontDown

Since the p elements itself doesn't have specific font-size style declared on the element, but instead in stylesheet, you can use getComputedStyle to get the font-size , here is more reference to that How To Get Font Size in HTML由于p元素本身没有在元素上声明特定的字体大小样式,而是在样式表中,您可以使用getComputedStyle来获取font-size ,这里有更多关于如何在 HTML 中获取字体大小的参考

 function fontUp(){ var fSize = document.getElementsByTagName('p'); $('#font-up').click(function(){ for(let i = 0; i < fSize.length; i++) { let currentSize = window.getComputedStyle(fSize[i], null).getPropertyValue('font-size'); fSize[i].style.fontSize = `${Number(currentSize.replace('px','')) + 1}px` } }); } function fontDn(){ var fSize = document.getElementsByTagName('p'); $('#font-dn').click(function(){ for(let i = 0; i < fSize.length; i++) { let currentSize = window.getComputedStyle(fSize[i], null).getPropertyValue('font-size'); fSize[i].style.fontSize = `${Number(currentSize.replace('px','')) - 1}px` } }); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="content"> <p id='p1'>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Qui iste deserunt voluptate unde, pariatur aliquam consequatur, aut neque accusamus consequuntur, odit velit exercitationem non eligendi fuga repudiandae! Omnis, veritatis officiis.</p> <br> <p id='p2'>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Qui iste deserunt voluptate unde, pariatur aliquam consequatur, aut neque accusamus consequuntur, odit velit exercitationem non eligendi fuga repudiandae! Omnis, veritatis officiis.</p> </div> <div id="selectors"> <button id="button1" class="color-button" onclick="changeColorRed()">Red</button> <button id="button2" class="color-button" onclick="changeColorBlue()">Blue</button> <button id="button3" class="color-button" onclick="changeColorGreen()">Green</button> <br> <br> <input id="red-input" class="input-box" type="text" placeholder="red"></input> <input id="green-input" class="input-box" type="text" placeholder="green"></input> <input id="blue-input" class="input-box" type="text" placeholder="blue"></input> <button id="rgb-button" class="color-button" onclick="changeRGB()">Change Background</button> <br> <br> <button id="font-up" onclick="fontUp()">Font Up</button> <button id="font-dn" onclick="fontDn()">Font Down</button> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM