Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jquery中鼠标悬停时的图像src更改_Jquery_Onmouseover - Fatal编程技术网

在jquery中鼠标悬停时的图像src更改

在jquery中鼠标悬停时的图像src更改,jquery,onmouseover,Jquery,Onmouseover,html- 但这不起作用,我想我写错了jquery????使用attr: <script> $('a.newslink1').bind('mouseover', function() { $('img#storyimg').src("images/stor1.jpg"); ...same for newslink2 and 3, image will be stor2 and 3 更多信息: $('a.newslink

html-

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

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

$('a.newslink1').bind('mouseover', function() {
$('img#storyimg').attr("src","images/stor1.jpg");
<script>
  $('a.newslink1').bind('mouseover', function() {
    $('img#storyimg').src("images/stor1.jpg");
您可能需要
$('img#storyimg').attr('src','path/to/new/image.jpg')

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


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

你的代码:

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

$('a.newslink1').bind('mouseover',function(){
$('img#storyimg').src(“images/stor1.jpg”);
错误:

$('a.newslink1').bind('mouseover', function() {
$('img#storyimg').attr("src","images/stor1.jpg");
<script>
  $('a.newslink1').bind('mouseover', function() {
    $('img#storyimg').src("images/stor1.jpg");
第3行:使用'attr'而不是'src'类似.attr(“src”,“images/stor1.jpg”);'

第4行:'});'在车站的尽头不见了

正确的代码:

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

$('a.newslink1').bind('mouseover',function(){
$('img#storyimg').attr(“src”,“images/stor1.jpg”);
});
如果要根据链接更改图像,可以编写以下代码:

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

//将mouseover事件处理程序绑定到具有类“sb_menu”的UL中LI的所有子链接 $('UL.sb_menu LI A').bind('mouseover',函数(e){ $('img#storyimg').attr(“src”,“images/”+$(e.target).attr('data-image')); });
一个改进是:“img#storyimg”作为选择器可以,但只有“#storyimg”更快,因为getElementById(..)是一个本机浏览器函数。如果使用“img#storyimg”,jquery必须请求getElementsByTagName('img')并遍历列表以查找id为“storyimg”的元素。这相当于直接执行“getElementById”所需的大量时间。
页面中任何HTML元素的ID都必须是unice。请参阅:(“此属性为元素指定名称。此名称在文档中必须是唯一的。”)

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

您还可以使用“.hover()”函数,可能如下所示:

这是介于
之间的一个:


$(文档).ready(函数(){
var src_path=“path/images/”;
var src_后缀=“.jpg”;
$('.yourclass')。悬停(
函数(){
$(此).addClass(“悬停”);
var active_id=$(this.attr('id');
$(“#”+active_id+“_pic”).attr(“src”,src_path+active_id+“_big”+src_后缀);
},
函数(){
$(this.removeClass(“hover”);
var active_id=$(this.attr('id');
$(“#”+active_id+“_pic”).attr(“src”,src_path+active_id+“_small”+src_后缀);
}
);
});
这是介于
之间的一个:


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

有关“.hover()”函数的详细信息,您可以在此处找到: