开发者

Javascript question regarding hiding certain divs by id thats a changing random number

Can anyone help me with javascript that hides divs matching a specific random number? Every time I load this page it has two divs like <div id='1945498985'> the number changes every page load but remains between 9-10 in length how would I fetch the two divs on the page and remove/hide them?

Let me clarify the two divs on this page each have a random number between 9 and 10 in length.

One such div looks like this:

<div id='843401712' style='position:relative;z-index: 1000;left: 75px;top: -340px; padding:0px; margin: 0px;width: 310px; height:280px; text-align: center; border:3px gray solid'></div>

I do not have control over the generated html I am looking to run javascript alongside a page as a greasemonkey extension to hide these two divs.

Thanks for any help you guys offer, I'm new to javascript so its a bi开发者_如何学编程g help.


Use document.getElementsByTagName() to fetch all div elements. Then, check their id using maybe a regular expression like [0-9]{9,10} and if the id matches, remove/hide them.


You have two problems with your question.

  • First, you're suggesting that you have two divs with the same ID, which is illegal.

  • Secondly, your ID is all numeric, which is also illegal.

Here (using jquery for brevity and assuming you're getting your random from a dynamically generated page like from PHP) is an example of doing what you're looking for:

<script type="text/javascript">
    var pRand = '<?php echo $pRand; ?>';
    document.ready(function(){
        $('.el-'+pRand).hide();
    });
</script>
...
<div class="el-<?php echo $pRand;?>"></div>
<div class="el-<?php echo $pRand;?>"></div>


Use jQuery filter and provide a function that does a regex match on your id:

http://api.jquery.com/filter#expr

See here: http://jsfiddle.net/ANW8C/

In your case...

Example:

$('div').filter(function() {
    return this.id.match('\\d{9,10}');
} ).hide();


I suggest to you using jquery it has some selector filters that may help you to selecting correct div

http://api.jquery.com/category/selectors/


As others have said, you should first make sure you have legal ID values. They cannot start with a number and there can only be one object with a given ID in the page.

If you have any control over the generated HTML, it would be best to put a common class name on all divs that you want to hide. You can then easily find them or add a style rule that will hide them.

<div id='a1945498985' class="hideMe"></div>
<div id='a2945498986' class="hideMe"></div>

The, you can either hide all objects with that class name. In jQuery, that would simply be this:

$(".hideMe").hide();


Here are some examples - I haven't used jQuery because you haven't suggested that you are using it. It is very useful for this kind of stuff, but you can also solve the problem without it if you need to.

http://jsfiddle.net/Sohnee/tpGqj/

There are two methods used, one relies on a parent container and the other uses the new getElementsByClassName support that some browsers have (you can roll your own getElementsByClassName if you need to support older browsers).

Ideally, you wouldn't apply a random id to an element - there seems little point in giving an element a random id. If you are setting the random id server side, you could also supply the id to the JavaScript so it can target the element with a getElementById call, which would be the most efficient.

Also, I concur with the statements about numeric ids being invalid - you should put at least one alphabetical character at the start of the id.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜