开发者

Javascript: How to display text if an image exists

开发者 https://www.devze.com 2023-02-24 03:53 出处:网络
I have been trying to create a code like this: If you show \'us.gif\' in the div id \'yourCountry\', display this css code:

I have been trying to create a code like this:

If you show 'us.gif' in the div id 'yourCountry', display this css code: --- #yourCountry img{display:none} --- End of the if

I have tried to do it and searched about it on the web, but nothing has worked for me.

This is how I was attempting to achieve it with javascript (I am a newbie with javascript):

<script language=javascript>
<!--
function u开发者_C百科simage ()  {
if document.images["us.gif"]  {
document.write("
<style type="text/css">
<!--
body div div div div div div div div form div div div table td img{display:none;} 
-->
</style>
");}}
</script>


That JS is a bit messy, would a CSS 2.1 selector be acceptable?

div#yourCountry img[src="us.gif"] { display:none; }


You can try:

<script language=javascript>
function usimage ()  {
    for(var i = 0; i < document.images.length; ++i)
    {
        if(document.images[i].src.search("us.gif") >= 0)
        {
            document.write('<style type="text/css">'
                +'<!--'
                +'body div div div div div div div div form div div div table td img{display:none;} '
                +'-->'
                +'</style>'
            );
        }
    }
}
</script>

P/S: You don't understand clearly about js language. Your code is wrong so much like you don't know any language.

0

精彩评论

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