我正在嘗試運行一個根據顯示的內容更改邊距頂部大小的函式。但是,它似乎不起作用?
<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>
謝謝
uj5u.com熱心網友回復:
document.getElementById回傳元素(如果存在)或 null,而不是布林值(真/假)。
你可以簡單地if(heading) { ... } 按照你的條件去做。
這是基于您的代碼的片段:https ://codepen.io/29b6/pen/XWZVqWx
uj5u.com熱心網友回復:
讓你的標簽<h1>之前的行代碼,如果還沒有定義,則為空:scriptheading
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/481478.html
標籤:javascript html css dom
上一篇:點擊時未呼叫列可點擊lambda塊-jetpackcompose
下一篇:在p5js上翻轉影像Y軸
