为什么我的字体尺寸不使用香草JS调整?

发布于 2025-01-25 08:33:46 字数 1569 浏览 3 评论 0原文

我在这里有这个小代码,通过控制台日志,我可以看到它显然已更改,但在显示器上没有更改。可能出了什么问题?

下方的JS香草

const gameItem = document.querySelectorAll('.game-item');
const gameItemS = document.querySelector('.game-item');

document.addEventListener('DOMContentLoaded', () => {
    titleSize(gameItem);
});

const titleSize = (gameItem) => {
    [...gameItem].forEach((title) => {
        let textSize = window
            .getComputedStyle(title, null)
            .getPropertyValue('font-size');
        let gameTitle = title.innerText;
        // console.log(gameTitle, gameTitle.length, textSize);
        if (gameTitle.length > 7) {
            textSize = '5px';
            console.log(
                'The font on this was changed: ' + gameTitle,
                gameTitle.length,
                textSize
            );
        }
    });
};

下面HTML

            <section class="games">
                <h1>Games</h1>
                <ol class="container">
                    <li class="game-item">Halo<img src="" /></li>
                    <li class="game-item">Pokemon<img src="" /></li>
                    <li class="game-item">
                        Animal Crossing<img src="" />
                    </li>
                    <li class="game-item">Genshin Impact<img src="" /></li>
                    <li class="game-item">Skyrim<img src="" /></li>
                </ol>
            </section>

I have this little code here and by the console logs I can see it is apparently changed but on the display it is not changing. What could be going wrong ?

JS vanilla below

const gameItem = document.querySelectorAll('.game-item');
const gameItemS = document.querySelector('.game-item');

document.addEventListener('DOMContentLoaded', () => {
    titleSize(gameItem);
});

const titleSize = (gameItem) => {
    [...gameItem].forEach((title) => {
        let textSize = window
            .getComputedStyle(title, null)
            .getPropertyValue('font-size');
        let gameTitle = title.innerText;
        // console.log(gameTitle, gameTitle.length, textSize);
        if (gameTitle.length > 7) {
            textSize = '5px';
            console.log(
                'The font on this was changed: ' + gameTitle,
                gameTitle.length,
                textSize
            );
        }
    });
};

html below

            <section class="games">
                <h1>Games</h1>
                <ol class="container">
                    <li class="game-item">Halo<img src="" /></li>
                    <li class="game-item">Pokemon<img src="" /></li>
                    <li class="game-item">
                        Animal Crossing<img src="" />
                    </li>
                    <li class="game-item">Genshin Impact<img src="" /></li>
                    <li class="game-item">Skyrim<img src="" /></li>
                </ol>
            </section>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2025-02-01 08:33:46

分配给textsize只会更改该变量的值,它对元素没有任何影响。当您进行时,让TextSize = /*....../..getPropertyValue('font-size');,它只是将来自计算样式对象的值复制到变量中。变量与计算样式对象之间没有持续的链接。 (即使有,分配给计算样式对象不会更改元素。)

要更改元素的样式,您也必须分配给title.Style.fontsize(或) title.Style [“ font-size”]):

const gameItem = document.querySelectorAll('.game-item');
const gameItemS = document.querySelector('.game-item');

document.addEventListener('DOMContentLoaded', () => {
    titleSize(gameItem);
});

const titleSize = (gameItem) => {
    [...gameItem].forEach((title) => {
        let textSize = window
            .getComputedStyle(title, null)
            .getPropertyValue('font-size');
        let gameTitle = title.innerText;
        // console.log(gameTitle, gameTitle.length, textSize);
        if (gameTitle.length > 7) {
            title.style.fontSize = '5px';
            console.log(
                'The font on this was changed: ' + gameTitle,
                gameTitle.length,
                textSize
            );
        }
    });
};
<section class="games">
    <h1>Games</h1>
    <ol class="container">
        <li class="game-item">Halo<img src="" /></li>
        <li class="game-item">Pokemon<img src="" /></li>
        <li class="game-item">
            Animal Crossing<img src="" />
        </li>
        <li class="game-item">Genshin Impact<img src="" /></li>
        <li class="game-item">Skyrim<img src="" /></li>
    </ol>
</section>

Assigning to textSize just changes the value of that variable, it doesn't have any effect on the element. When you do let textSize = /*...*/.getPropertyValue('font-size');, that just copies the value from the computed style object into the variable. There's no ongoing link between the variable and that computed style object. (Even if there were, assigning to the computed style object won't change the element.)

To change the element's style, you'd have to assign to title.style.fontSize (or title.style["font-size"]):

const gameItem = document.querySelectorAll('.game-item');
const gameItemS = document.querySelector('.game-item');

document.addEventListener('DOMContentLoaded', () => {
    titleSize(gameItem);
});

const titleSize = (gameItem) => {
    [...gameItem].forEach((title) => {
        let textSize = window
            .getComputedStyle(title, null)
            .getPropertyValue('font-size');
        let gameTitle = title.innerText;
        // console.log(gameTitle, gameTitle.length, textSize);
        if (gameTitle.length > 7) {
            title.style.fontSize = '5px';
            console.log(
                'The font on this was changed: ' + gameTitle,
                gameTitle.length,
                textSize
            );
        }
    });
};
<section class="games">
    <h1>Games</h1>
    <ol class="container">
        <li class="game-item">Halo<img src="" /></li>
        <li class="game-item">Pokemon<img src="" /></li>
        <li class="game-item">
            Animal Crossing<img src="" />
        </li>
        <li class="game-item">Genshin Impact<img src="" /></li>
        <li class="game-item">Skyrim<img src="" /></li>
    </ol>
</section>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文