更改div中的字体大小

问题描述 投票:-1回答:1

我使用以下代码来更改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>
javascript jquery html css font-size
1个回答
1
投票

正如评论中所提到的,问题在于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>
© www.soinside.com 2019 - 2024. All rights reserved.