通过ID更改页脚大小

发布于 2025-02-02 14:27:43 字数 579 浏览 3 评论 0原文

我正在尝试根据所显示的功能运行一个函数,该函数会更改边距大小。但是,它似乎不起作用吗?

<body onload="changeFooter()">

<script>

const heading = document.getElementById('verify'); 
const footer = document.getElementById('footer')


function changeFooter () {
   if (heading == true){
       footer.style.marginTop = "200px"
   }
}

也尝试了此

function changeFooter () {
   if (heading.match('Verify')){
       footer.style.marginTop = "200px"
   }
}



</script>


 <h1 id="verify" class="verifyheading">Verify Identity</h1>

谢谢

I am trying to run a function that changes the margin-top size according to what is being displayed. However, it doesn't seem to be working?

<body onload="changeFooter()">

<script>

const heading = document.getElementById('verify'); 
const footer = document.getElementById('footer')


function changeFooter () {
   if (heading == true){
       footer.style.marginTop = "200px"
   }
}

also tried this

function changeFooter () {
   if (heading.match('Verify')){
       footer.style.marginTop = "200px"
   }
}



</script>


 <h1 id="verify" class="verifyheading">Verify Identity</h1>

Thank you

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

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

发布评论

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

评论(2

笑叹一世浮沉 2025-02-09 14:27:43

document.getElementById返回元素(如果存在)或null,而不是布尔值(true/false)。

您可以简单地做,如果(标题){...}作为您的条件。

这是基于您的代码的摘要: https://codepen.io/29b6/pen/pen/pen/pen/pen/pen/pen/xwzvqqwx

document.getElementById returns the element (if it exists) or null, not a boolean (true/false).

You can simply do if(heading) { ... } as your condition.

Here's a snippet based on your code: https://codepen.io/29b6/pen/XWZVqWx

清浅ˋ旧时光 2025-02-09 14:27:43

在您的&lt; h1&gt;的行代码脚本标签之前,标题&gt;之前将行代码取消:如果还没有定义:

<body onload="changeFooter()">

 <h1 id="verify" class="verifyheading">Verify Identity</h1>
 <div id="footer">Footer with marginTop</div>

<script>

const heading = document.getElementById('verify'); 
const footer = document.getElementById('footer')


function changeFooter () {
   if (heading){
       footer.style.marginTop = "200px"
   }
}

</script>

Let put line code of <h1> before your script tag, heading will null if not define yet:

<body onload="changeFooter()">

 <h1 id="verify" class="verifyheading">Verify Identity</h1>
 <div id="footer">Footer with marginTop</div>

<script>

const heading = document.getElementById('verify'); 
const footer = document.getElementById('footer')


function changeFooter () {
   if (heading){
       footer.style.marginTop = "200px"
   }
}

</script>

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