当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Hide content when URL is less/more than X characters long

问题描述:

I have an html document of which I want to hide some ids (in this example, id="test") when the URL is less than 30 characters long.

var test = document.getElementById('test');

var l = location.href.length;

if(l<30){function hide1(){test.style.display="none";}};

Any idea why this is not working?

网友答案:

You're declaring a function and not calling it. And you don't need it anyway.

Replace

if(l<30){function hide1(){test.style.display="none";}};

with

if(l<30) test.style.display="none";
网友答案:

you should do something like this

function hideAnchorWithLengthLessThan (anchor, length) {
    if (anchor.href.length < length) {
        anchor.style.display = 'none';
    }
}

than you can do

var test = document.getElementById('test');
hideAnchorWithLengthLessThan(test, 30);
分享给朋友:
您可能感兴趣的文章:
随机阅读: