jquery中鼠标悬停时图像src发生变化

发布于 2024-09-13 05:09:10 字数 985 浏览 7 评论 0原文

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?????????

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>

I want when user moves over these li items I want to change the image like-

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

but this is not working i think i have written wrong jquery?????????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

红衣飘飘貌似仙 2024-09-20 05:09:10

使用attr

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

更多信息:

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

Use attr:

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

More Info:

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

唠甜嗑 2024-09-20 05:09:10

您的代码:

<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 都必须是 unice。请参阅:http://www.w3.org/ TR/html401/struct/global.html#h-7.5.2(“该属性为元素分配一个名称。该名称在文档中必须是唯一的。”)

Your code:

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

Errors:

Line 3: use 'attr' instead of 'src' like '.attr("src","images/stor1.jpg");'

Line 4: ' }); ' is missing at end of the statment

Correct code:

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

If you want to change the image depend on the link you can code:

<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>

An improve: "img#storyimg" as selector is ok but only "#storyimg" is mutch faster because getElementById(..) is an native-browser-function. If you use "img#storyimg" jquery must request getElementsByTagName('IMG') and traverse the list to find this element with the id "storyimg". this takes a lot of time equals to the direct execution of "getElementById".
An ID of any HTML-Element in a page must be unice. see: http://www.w3.org/TR/html401/struct/global.html#h-7.5.2 ("This attribute assigns a name to an element. This name must be unique in a document.")

终弃我 2024-09-20 05:09:10
$('a.newslink1').bind('mouseover', function() {
$('img#storyimg').attr("src","images/stor1.jpg");
$('a.newslink1').bind('mouseover', function() {
$('img#storyimg').attr("src","images/stor1.jpg");
梦里兽 2024-09-20 05:09:10

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

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

还有一件事,尝试一下 .mouseenter()mouseleave()

You probably want $('img#storyimg').attr('src','path/to/new/image.jpg');

EDIT: JINX gotta but me a coke! :o)

one more thing, experiment with .mouseenter() and mouseleave().

疾风者 2024-09-20 05:09:10

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

您还可以使用“.hover()”函数,可能像这样:

之间的这个:

<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>

以及 < code> 和

<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 /悬停/

I know it's long ago that the question was asked, but maybe someone could need some other solutions. So I thought, maybe I could help too.

You could also use the ".hover()" function, maybe like this:

This one between <head> and </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>

And this one between <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>

On one of our websites, it works well.

More Information about the ".hover()" function, you could find here: http://api.jquery.com/hover/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文