问题描述:

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);
相关阅读:
Top