2

html-

<img id="storyimg" src="images/stor.jpg" alt="img" />  
                <ul class="sb_menu">            
                    <li><a href="linkpage.htm" class="newslink1">Wireless Networking at Fortune Inn, Noida</a></li>
                    <li><a href="linkpage.htm" class="newslink2">18th International Conference on Oral & Maxillofacial Surgery</a></li>
                    <li><a href="linkpage.htm" class="newslink3">WiFi deployment at Vellore Institute of Technology</a></li>                        
                </ul>

我想当用户移动这些li项目时,我想更改图像,例如-

<script>
                $('a.newslink1').bind('mouseover', function() {
                $('img#storyimg').src("images/stor1.jpg");
...same for newslink2 and 3, image will be stor2 and 3

但这不起作用我想我写错了jquery?????????

4

5 回答 5

12

使用attr

$('img#storyimg').attr("src", "images/stor1.jpg");

更多信息:

http://api.jquery.com/attr/

于 2010-08-05T15:50:51.820 回答
4

你的代码:

<script>
  $('a.newslink1').bind('mouseover', function() {
    $('img#storyimg').src("images/stor1.jpg");

错误:

第 3 行:使用 'attr' 而不是 'src',例如 '.attr("src","images/stor1.jpg");'

第 4 行:' }); ' 在语句末尾缺少

正确代码:

<script>
  $('a.newslink1').bind('mouseover', function() {
    $('img#storyimg').attr("src","images/stor1.jpg");
  });

如果要更改图像取决于您可以编码的链接:

<img id="storyimg" src="images/stor.jpg" alt="img" />  
<ul class="sb_menu">            
  <li><a href="linkpage.htm" class="newslink1" data-image="stor1.jpg">Wireless Networking at Fortune Inn, Noida</a></li>
  <li><a href="linkpage.htm" class="newslink2" data-image="stor2.jpg">18th International Conference on Oral & Maxillofacial Surgery</a></li>
  <li><a href="linkpage.htm" class="newslink3" data-image="stor3.jpg">WiFi deployment at Vellore Institute of Technology</a></li>                        
</ul>

<script>
  //binds the mouseover-event-handler to all Links the are childs of an LI in UL with Class "sb_menu"
  $('UL.sb_menu LI A').bind('mouseover', function(e) { 
    $('img#storyimg').attr("src","images/" + $(e.target).attr('data-image'));
  });
</script>

改进:“img#storyimg”作为选择器没问题,但只有“#storyimg”更快,因为 getElementById(..) 是本机浏览器功能。如果使用“img#storyimg”,jquery 必须请求 getElementsByTagName('IMG') 并遍历列表以找到 id 为“storyimg”的元素。这需要很多时间,相当于直接执行“getElementById”。页面中任何 HTML 元素的 ID 必须是统一的。请参阅:http ://www.w3.org/TR/html401/struct/global.html#h-7.5.2 (“此属性为元素分配名称。此名称在文档中必须是唯一的。”)

于 2010-08-05T15:55:24.277 回答
2
$('a.newslink1').bind('mouseover', function() {
$('img#storyimg').attr("src","images/stor1.jpg");
于 2010-08-05T15:51:37.733 回答
1

你可能想要$('img#storyimg').attr('src','path/to/new/image.jpg');

编辑: JINX 得给我一杯可乐!:o)

还有一件事,试验.mouseenter()and mouseleave()

于 2010-08-05T15:52:12.643 回答
1

我知道很久以前就有人问过这个问题,但也许有人可能需要其他解决方案。所以我想,也许我也可以帮忙。

你也可以使用“.hover()”函数,可能是这样的:

这个介于<head>和之间</head>

<script type="text/javascript">
    $(document).ready(function() {
        var src_path = "path/images/";
        var src_suffix = ".jpg";                   
        $('.yourclass').hover(                         
            function () {
            $(this).addClass("hover");
            var active_id = $(this).attr('id');     
            $('#' + active_id + '_pic').attr("src", src_path + active_id + '_big' + src_suffix); 
            },
            function () {
            $(this).removeClass("hover");
            var active_id = $(this).attr('id');
            $('#' + active_id + '_pic').attr("src", src_path + active_id + '_small' + src_suffix);
            }
        );
    });
</script>

而这个介于<body>and之间</body>

<div class="fruits">
<a href="#" class="yourclass" id="apple">
<img id="apple_pic" src="files/images/apple_small.jpg" alt="apple" title="apple" />
</a>
<!--  -->
<a href="#" class="yourclass" id="pear">
<img id="pear_pic" src="files/images/pear_small.jpg" alt="pear" title="pear" />
</a>
</div>

在我们的一个网站上,它运行良好。

关于“.hover()”函数的更多信息,你可以在这里找到:http: //api.jquery.com/hover/

于 2013-02-01T10:16:18.867 回答