在下面的代码中,我希望在页面加载时显示第一个图像,但是,它不会显示任何内容,并且Firebug中没有错误.
我怎样才能使这条线工作:
$('img#main').attr('src', $('img:first').src);
Run Code Online (Sandbox Code Playgroud)
完整代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('jquery', '1.5');
google.setOnLoadCallback(function() {
$('img.thm').css({opacity: 0.7});
$('img#main').attr('src', $('img:first').src); //doesn't work
$('img.thm').width(80).click(function() {
$('img#main').attr('src', this.src);
});
$('img.thm').mouseover(function() {
$(this).css({opacity: 1.0});
});
$('img.thm').mouseout(function() {
$(this).css({opacity: 0.7});
});
});
</script>
<style type="text/css">
img.thm {
cursor: hand;
cursor: pointer;
}
</style>
</head>
<body>
<div id="menu">
<img class="thm" src="images/test1.png"/>
<img class="thm" src="images/test2.png"/>
<img class="thm" src="images/test3.png"/>
</div>
<div id="content">
<img id="main"/>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
.attr()从获取$('img:first')jQuery对象的图像源时使用:
$('img#main').attr('src', $('img:first').attr('src'));
Run Code Online (Sandbox Code Playgroud)
该.src属性用于表示图像的DOM对象.你可以像这样使用它(见.get()):
$('img#main').attr('src', $('img').get(0).src);
Run Code Online (Sandbox Code Playgroud)
但为了保持一致,我会用.attr().
| 归档时间: |
|
| 查看次数: |
17638 次 |
| 最近记录: |