简体   繁体   English

Javascript:如果图像存在,如何显示文本

[英]Javascript: How to display text if an image exists

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 如果在div ID“ yourCountry”中显示“ us.gif”,请显示以下CSS代码:--- #yourCountry img {display:none} --- 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): 这就是我试图用javascript实现的方式(我是javascript的新手):

<script language=javascript>
<!--
function usimage ()  {
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>

JS有点混乱,可以接受CSS 2.1选择器吗?

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. P / S:您不太了解js语言。 Your code is wrong so much like you don't know any language. 您的代码是错误的,就像您不懂任何语言一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM