是否可以在CSS中设置src属性值? 在大多数情况下,我们这样使用它:

<img src="pathTo/myImage.jpg" />

我希望它是这样的

<img class="myClass" />
.myClass {
    some-src-property: url("pathTo/myImage.jpg");
}

我想知道是否有一种方法可以做到这一点,而不使用CSS中的background或background-image属性。


当前回答

在一个“控制”容器中放置几张图像,并更改容器的类。在CSS中,根据容器的类添加规则来管理图像的可见性。这将产生与改变单个图像的img src属性相同的效果。

HTML:

<span id="light" class="red">
    <img class="red" src="red.png" />
    <img class="yellow" src="yellow.png" />
    <img class="green" src="green.png" />
</span>

CSS:

#light         { ... }
#light         *        { display: none; }     // all images are hidden
#light.red     .red     { display: inline; }   // show red image when #light is red
#light.yellow  .yellow  { display: inline; }   // .. or yellow
#light.green   .green   { display: inline; }   // .. or green

注意,它将预加载所有图像,就像CSS的backround-images,但不像通过JS更改img src。

其他回答

我想补充一点:背景图像也可以用background-position: x - y来定位;(x水平y垂直)。(. .) 我的例子,CSS:

(..) 
#header {
  height: 100px; 
  background-image: url(http://.../head6.jpg); 
  background-position: center; 
  background-repeat: no-repeat; 
  background-color: grey; 
  (..)
} 
(...)

不,你不能通过CSS设置图像src属性。你能得到的最接近的是,如你所说,背景或背景图像。我不建议这样做,因为这有点不合逻辑。

但是,如果你的目标浏览器能够使用CSS3解决方案,那么你可以使用它。请使用Pacerier回答中描述的content:url。您可以在下面的其他答案中找到其他跨浏览器的解决方案。

他们是对的。IMG是内容元素,CSS是设计元素。 但是,当您出于设计目的使用某些内容元素或属性时,情况又会如何呢? 我有IMG跨我的网页,必须改变,如果我改变样式(CSS)。

好吧,这是一个解决方案定义IMG表示(不是真正的图像)在CSS风格。

创建一个1x1透明GIF或png。 将IMG的属性“src”分配给该图像。 在CSS样式中用“background-image”定义最终的表示形式。

它的工作就像一个魅力:)

HTMl代码:

<!DOCTYPE html>
<html>
     <head>
         <link type="text/css" rel="stylesheet" href="css destination" />
     </head>
     <body>
<!-- click-able pic with link -->
           <a href="site you want"> 
<!-- Take the off if you don't want click-able link -->
                <h1 id(or class)="nameOfClassorid">
                     <span>Text that is not important</span>
                </h1>
           </a>
      </body>
 </html>

Css代码:

span {
     display: none;
}
h1 id or class {
     height: of pic;
     width: of pic;
/* Only flaw (so far) read bottom */
     background-image:url(/* "image destination" */);
}
h1 id or class:hover {
/* Now the awesome part */
     background-image:url(/* 'new background!!!' */);
}

放学后我一直在学习html几天,想知道如何做到这一点。找出背景,然后把两者结合起来。 这工作100%我检查,如果不确保你填写必要的东西!! 我们需要指定高度,因为没有它就什么都没有!! 我留下这个基本的外壳,你可以添加。

例子:

 <!DOCTYPE html>
 <html>
     <head>
         <link type="text/css" rel="stylesheet" href="style.css" />
     </head>
     <body>
           <a href="http:localhost"> 
                <h1>
                     <span>Text that is not important</span>
                </h1>
           </a>
     </body>
 </html>
span {
     display: none;
}
h1 {
     height: 100px;
     width: 100px;
     background-image:url("http://linuxlog.org/wp-content/uploads/2011/01/Ubuntu-Desktop-@-2011-01-11-191526-300x225.png");
}

h1:hover {
     height: 300px;
     width: 300px;
     background-image:url("http://cdn.css-tricks.com/images/ads/wufoo-600x600-red.png");
}

附注:是的,我是Linux用户;)

如果你不想设置背景属性,那么你不能只使用CSS来设置图像的src属性。

您也可以使用JavaScript来完成这一任务。