jquery中鼠标悬停时图像src发生变化
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
attr
:更多信息:
http://api.jquery.com/attr/ jquery.com/attr/
Use
attr
:More Info:
http://api.jquery.com/attr/
您的代码:
错误:
第 3 行:使用 'attr' 而不是 'src',如 '.attr("src","images/stor1.jpg");'
第 4 行:' });语句末尾缺少'
正确代码:
如果您想根据链接更改图像,您可以编码:
改进:“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:
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:
If you want to change the image depend on the link you can code:
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.")
你可能想要
$('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()
andmouseleave()
.我知道这个问题很久以前就被提出了,但也许有人可能需要其他解决方案。所以我想,也许我也能帮忙。
您还可以使用“.hover()”函数,可能像这样:
和
之间的这个:
以及 < code> 和
:
在我们的一个网站上,它运行良好。
有关“.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>
:And this one between
<body>
and</body>
:On one of our websites, it works well.
More Information about the ".hover()" function, you could find here: http://api.jquery.com/hover/