如何在css中更改img的src

html-css03

如何在css中更改img的src,第1张

<html>

<head>

<style>

img{

text:expression(src="b.png")

}

</style>

<body>

<img src="a.jpg">

</body>

</head>

</html>

css改src图只有ie支持 或者css控制背景图background-image好些

一般都是js

document.getElementById("imgid").src = "b.png"

你可以使用jq来改变,,

css:

<style>

.div{

background:url("test.jpg") no-repeat

border:solid 1px red

height:200px

}

</style>

<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>

<script language="javascript">

$(document).ready(function(){

$(".div").click(function(){          //写了点击测试效果

$(this).css({

"background" : "url('menu.jpg') no-repeat",

"borderColor" : "blue"

})

})

})

</script>

html:

<div class="div" onclick="changeBg(this)"></div>

测试:

初始

点击后

js:

--------------------------------------------------------------- 

function changeBg(obj){ 

    obj.style.backgroundImage="url(menu.jpg)"             //都是使用驼峰写法~~~

---------------------------------------------------------------

设置属性值以 "turntable" 开头的所有 img 元素的样式,

就是选择所有IMG标签里SRC=URL(URL是以turntable开头的)

img[src^="https"]

选择其 src 属性值以 "https" 开头的每个<img>元素。

img[src$=".jpg"]

选择其 src 属性以 ".jpg" 结尾的所有<img>元素。

img[src*="abc"]

选择其 src 属性中包含 "abc" 子串的每个<img>元素