【问题标题】:Change font size in a div更改 div 中的字体大小
【发布时间】:2019-07-10 01:41:33
【问题描述】:

我正在使用以下代码更改bodytext div 的字体大小。但是,当我尝试使用具有各种格式的页面时,并且在 <div id="bodytext"> 内它不起作用,我不知道为什么。它只是改变线条之间的间距或其他东西..

工作脚本:

var $affectedElements = $("#bodytext"); // Can be extended, ex. $("div, p, span.someClass")

// Storing the original size in a data attribute so size can be reset
$affectedElements.children().each( function(){
  var $this = $(this);
  $this.data("orig-size", $this.css("font-size") );
});

$("#btn-increase").click(function(){
  changeFontSize(1);
})

$("#btn-decrease").click(function(){
  changeFontSize(-1);
})

$("#btn-orig").click(function(){
  $affectedElements.children().each( function(){
        var $this = $(this);
        $this.css( "font-size" , $this.data("orig-size") );
   });
})

function changeFontSize(direction){
    $affectedElements.children().each( function(){
        var $this = $(this);
        $this.css( "font-size" , parseInt($this.css("font-size"))+direction );
    });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<button id="btn-decrease">A-</button>
<button id="btn-orig">A</button>
<button id="btn-increase">A+</button>

<div id="bodytext">
  <p style="font-size : 30px">This text is initially 30px</p>
    <p style="font-size : 20px">This text is initially 20px</p>
    <p style="font-size : 10px">This text is initially 10px</p>
    
  </div>  
</div>

不工作(需要修复):

var $affectedElements = $("#bodytext"); // Can be extended, ex. $("div, p, span.someClass")

// Storing the original size in a data attribute so size can be reset
$affectedElements.children().each( function(){
  var $this = $(this);
  $this.data("orig-size", $this.css("font-size") );
});

$("#btn-increase").click(function(){
  changeFontSize(1);
})

$("#btn-decrease").click(function(){
  changeFontSize(-1);
})

$("#btn-orig").click(function(){
  $affectedElements.children().each( function(){
        var $this = $(this);
        $this.css( "font-size" , $this.data("orig-size") );
   });
})

function changeFontSize(direction){
    $affectedElements.children().each( function(){
        var $this = $(this);
        $this.css( "font-size" , parseInt($this.css("font-size"))+direction );
    });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>


<button id="btn-decrease">A-</button>
<button id="btn-orig">A</button>
<button id="btn-increase">A+</button>

<div id="bodytext">

<blockquote>
    <h1 dir="ltr" align="center">
    <span style="font-size: 35pt" lang="ar-sa">title page</span></h1>		
		<p class=MsoNormal dir=ltr style='text-align:justify'><b>
		<font size="5">4. some text: &quot; more text</font></span></b><font size="5"><span
lang=ar-eg> <b>more words</b></span><b>&quot;</span></b></font><b><font size="5">[6].</font></span></b></p>
		<p class=MsoNormal dir=ltr style='text-align:justify'>
		<font size="5">new sentence..................</font>
		<font style="font-size: 18pt" SIZE="5">
		<a style="font-size:18pt;" href="http://example.com/file.html">a link</a></font><font size="5">texttexttext.......</font><font SIZE="5" face="Times New Roman">
		<span lang="AR-EG" style="font-size: 18pt; ">
		<a href="http://www.example.com/file.php">a link</a></span></font><font size="5">words words words words words ..</font></span></p>
		<h2 dir=ltr>&nbsp;</h2>
	<h2 dir=ltr><b><span style="font-size: 22pt; font-style: normal">
	<a name="text">other text</a></span></b></h2>
		<p class=MsoNormal dir=ltr style='text-align:justify'><b>
		<font size="5">&quot;final words....</font></span></b><font size="5"><b>&quot;</span></b></font><b><font size="5">[7].</font></span></b></p>
</blockquote>  
</div>

【问题讨论】:

  • “损坏”版本中有很多 HTML 错误。使用带有语法突出显示的编辑器,您会看到它们。我建议先修复它们。
  • 您的代码示例设置为仅更新 #bodytext 的子元素的字体大小。如果一个元素在你的&lt;div id="bodytext"&gt; 中的深度超过 2 层,它们将不会获得应用到它们的新字体大小。
  • @RoryMcCrossan 我尝试通过 jsfiddle 删除一些错误,但同样的问题仍然存在:jsfiddle.net/0fxqarws
  • @MarioD 即使我将其更改为$affectedElements.each( function(){ 等等,但它仍然不起作用。

标签: javascript jquery html css font-size


【解决方案1】:

如 cmets 中所述,问题出在 var $affectedElements = $("#bodytext"); 选择器上。此选择器仅选择 blockquote 元素,因为它是 #bodytext 元素的唯一直接兄弟。

这意味着您只更改blockquote 元素的字体大小。浏览器的 dom 具有层叠效果,这是 CSS 所坚持的。因此,如果您将 13px 的字体大小应用到 blockquote 元素,然后在 dom 中将内联样式应用到 blockquote 元素的同级元素之一,则该内联样式将优先。

我在下面所做的是将* 选择器添加到$("#bodytext") 选择器,它将选择#bodytext 内的所有元素。我这样做是为了表明问题出在选择器上。不过,我建议考虑一种更好的方法来选择您需要的特定元素或删除 HTML 中的内联样式。

注意:我对 HTML 所做的唯一更改是清理损坏的标签。

var $affectedElements = $("#bodytext *"); // Can be extended, ex. $("div, p, span.someClass")

// Storing the original size in a data attribute so size can be reset
$affectedElements.children().each( function(){
  var $this = $(this);
  $this.data("orig-size", $this.css("font-size") );
});

$("#btn-increase").click(function(){
  changeFontSize(1);
})

$("#btn-decrease").click(function(){
  changeFontSize(-1);
})

$("#btn-orig").click(function(){
  $affectedElements.children().each( function(){
        var $this = $(this);
        $this.css( "font-size" , $this.data("orig-size") );
   });
})

function changeFontSize(direction){
    $affectedElements.children().each( function(){
        var $this = $(this);
        $this.css( "font-size" , parseInt($this.css("font-size"))+direction );
    });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<button id="btn-decrease">A-</button>
<button id="btn-orig">A</button>
<button id="btn-increase">A+</button>

<div id="bodytext">
  
<blockquote>
    <h1 dir="ltr" align="center">
    <span style="font-size: 35pt" lang="ar-sa">title page</span></h1>		
		<p class=MsoNormal dir=ltr style='text-align:justify'><b>
		<font size="5">4. some text: &quot; more text</font></b><font size="5"><span
lang=ar-eg> <b>more words</b></span><b>&quot;</b></font><b><font size="5">[6].</font></b></p>
		<p class=MsoNormal dir=ltr style='text-align:justify'>
		<font size="5">new sentence..................</font>
		<font style="font-size: 18pt" SIZE="5">
		<a style="font-size:18pt;" href="http://example.com/file.html">a link</a></font><font size="5">texttexttext.......</font><font SIZE="5" face="Times New Roman">
		<span lang="AR-EG" style="font-size: 18pt; ">
		<a href="http://www.example.com/file.php">a link</a></span></font><font size="5">words words words words words ..</font></p>
		<h2 dir=ltr>&nbsp;</h2>
	<h2 dir=ltr><b><span style="font-size: 22pt; font-style: normal">
	<a name="text">other text</a></span></b></h2>
		<p class=MsoNormal dir=ltr style='text-align:justify'><b>
		<font size="5">&quot;final words....</font></b><font size="5"><b>&quot;</b></font><b><font size="5">[7].</font></b></p>
  </blockquote>   
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2011-12-15
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    相关资源
    最近更新 更多