如何使用JavaScript更改子元素的字体大小 [英] How to change font size of sub child element using javascript

查看:82
本文介绍了如何使用JavaScript更改子元素的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似下面的html

I have html like below

<span id="3072" style="position: relative; display: inline-block; width: 1898px; height: 194px; ">
     <div style="visibility: visible; text-align: center; position: absolute; top: 90.5px; width: 1898px;">
         <div style="font-family: Arial; font-size: 8.25pt; font-style: normal;>
         </div>
     </div>
</span>

在这里我想将最后一个div标签的font-size:8.25pt更改为14pt,如何使用span标签中的id访问最后一个div标签的内容.

here I want to change font-size:8.25pt to 14pt from the last div tag, how do I access contents of the last div tag using id from span tag.

我可以轻松更改第一个div标签的内容,如下所示

I can easily change the contents of the 1st div tag something like below

document.getElementById("3072").firstChild.fontize="14px";

但是在这种情况下,Div标签没有任何类或ID可以访问它们.

But in this case the Div tags doesn't have any classes or Id's to access them.

任何人对此都有任何想法,请分享.

Anyone has any thoughts on this please share.

所附图片显示了html的代码.

Image attached which shows the code of the html.

谢谢.

推荐答案

最简单的方法是将类或id直接添加到目标元素中,但是如果您不能更改标记,则可以使用:-

The easiest way is to add a class or id directly to your target element, but if you can not change the markup then you can use:-

使用querySelector

document.querySelector('[id="3072"]>div>div').style['font-size'] = '14px';

<span id="3072" style="position: relative; display: inline-block; width: 1898px; height: 194px; ">
     <div style="visibility: visible; text-align: center; position: absolute; top: 90.5px; width: 1898px;">
         <div style="font-family: Arial; font-size: 8.25pt; font-style: normal;">
           14px
         </div>
     </div>
</span>

或jQuery

$('#3072>div>div').css('font-size', '14px');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="3072" style="position: relative; display: inline-block; width: 1898px; height: 194px; ">
  <div style="visibility: visible; text-align: center; position: absolute; top: 90.5px; width: 1898px;">
    <div style="font-family: Arial; font-size: 8.25pt; font-style: normal;">
      14px
    </div>
  </div>
</span>

注意:请参见此处有关将数字用作ID.

Note: see here about using numbers as id's.

这篇关于如何使用JavaScript更改子元素的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆