Javascript 将变量传递给this.src

Javascript 将变量传递给this.src,javascript,jquery,html,image,onmouseover,Javascript,Jquery,Html,Image,Onmouseover,我有一个用javascript生成的图像文件的名称,并以HTML的形式传递给图像的src——这目前是有效的 我想传递另一个图像文件作为图像的onmouseover属性。 (因为我的文件名是动态生成的,所以不能在css中使用hover) 以及: 但是我不知道如何在this.src属性中传递我的新变量 有人有什么想法吗 非常感谢 我相信这就是你要找的。不需要jQuery,只需要简单的旧JavaScript const image = document.querySelector('img');

我有一个用javascript生成的图像文件的名称,并以HTML的形式传递给图像的src——这目前是有效的

我想传递另一个图像文件作为图像的onmouseover属性。 (因为我的文件名是动态生成的,所以不能在css中使用hover)

以及:


但是我不知道如何在this.src属性中传递我的新变量

有人有什么想法吗


非常感谢

我相信这就是你要找的。不需要jQuery,只需要简单的旧JavaScript

const image = document.querySelector('img');

const baseImagePath = 'https://unsplash.it/';

const mouseEnterImage = '300';

const mouseLeaveImage = '400';

const replaceImage = (withImg) => {
    image.src = `${baseImagePath}${withImg}`;
}

document.addEventListener('mouseenter', () => replaceImage(mouseEnterImage));

document.addEventListener('mouseleave', () => replaceImage(mouseLeaveImage));
这里有一把小提琴在工作:


希望这能有所帮助。

使用这些事件处理程序的更好方法,
onmouseover
onmouseout
是为它们提供在脚本中定义的各自的函数。您还需要将图像正确地分配给
img
元素(
ev.srcElement
)查看:

<script>
MouseOverHandler = function(ev){ 
    ev.srcElement.src = new_source_for_image
    console.log('should change',ev);
}

MouseOutHandler = function(ev){ 
    ev.srcElement.src = old_source_for_image
    console.log('should change',ev);
}
</script>

<a href="#"><img id="visit_image" src="" width="350" height="350" 
             onmouseover="MouseOverHandler(this)"
             onmouseout="MouseOutHandler(this)"></a>

MouseOverHandler=函数(ev){
ev.srcielement.src=新的\u源\u用于\u映像
console.log('should change',ev);
}
MouseOutHandler=函数(ev){
ev.srcielement.src=旧\u源\u用于\u映像
console.log('should change',ev);
}

通过使用
mouseover()
mouseout()
attr()
方法使用jQuery

$(document).ready(function(){
  var file_name='your_file_name';
  var new_source_for_image ="/prestashop_1.7.0.3/modules/se_daynight/img/" + file_name + ".png";
  var new_source_for_image_with_glow = "/prestashop_1.7.0.3/modules/se_daynight/img/" + file_name + "_glow" + ".png";

  $("img#visit_image").attr('src',new_source_for_image);
  $("img#visit_image").mouseover(function(){
    $(this).attr('src',new_source_for_image_with_glow);
  });

  $("img#visit_image").mouseout(function(){
    $(this).attr('src',new_source_for_image);
  });
});
这将有助于:

 <script>
        var new_source_for_image_with_glow = "testA.png";

        var anotherSrc = "testB.png";


        function onmouseoverFunc(element) {
            element.src = new_source_for_image_with_glow;
        }

        function onmouseoutFunc(element) {
            element.src = anotherSrc;
        }
    </script>


<a href="#">
    <img id="visit_image" src="new_source_for_image_with_glow" width="350" height="350"
         onmouseover="onmouseoverFunc(this)"
         onmouseout="onmouseoutFunc(this)">
</a>

var new_source_用于图像_,带有\u glow=“testA.png”;
var anotherSrc=“testB.png”;
函数onmouseoverFunc(元素){
element.src=带有发光的图像的新光源;
}
函数onmouseoutunc(元素){
element.src=另一个src;
}

如果您可以动态设置
src
。。。我想您也可以为“备用源”设置数据属性

//模拟图像的动态文件源。
$(“#访问_图像”).attr(“src”http://lorempixel.com/400/200/sports");
//同样,您可以设置“备用源”。。。
$(“#访问_图像”)。数据(“altSrc”,”http://lorempixel.com/400/200/animals");
//鼠标事件处理程序
$(“#访问_图像”)。在(“mouseenter mouseleave”,function()上{
var src=$(this.attr(“src”);
var altSrc=$(this).data(“altSrc”);
//交换网址
$(this.attr(“src”,altSrc).data(“altSrc”,src);
});

关于术语
mouseover()
mouseout()
attr()
是方法,而不是函数。
<script>
MouseOverHandler = function(ev){ 
    ev.srcElement.src = new_source_for_image
    console.log('should change',ev);
}

MouseOutHandler = function(ev){ 
    ev.srcElement.src = old_source_for_image
    console.log('should change',ev);
}
</script>

<a href="#"><img id="visit_image" src="" width="350" height="350" 
             onmouseover="MouseOverHandler(this)"
             onmouseout="MouseOutHandler(this)"></a>
$(document).ready(function(){
  var file_name='your_file_name';
  var new_source_for_image ="/prestashop_1.7.0.3/modules/se_daynight/img/" + file_name + ".png";
  var new_source_for_image_with_glow = "/prestashop_1.7.0.3/modules/se_daynight/img/" + file_name + "_glow" + ".png";

  $("img#visit_image").attr('src',new_source_for_image);
  $("img#visit_image").mouseover(function(){
    $(this).attr('src',new_source_for_image_with_glow);
  });

  $("img#visit_image").mouseout(function(){
    $(this).attr('src',new_source_for_image);
  });
});
 <script>
        var new_source_for_image_with_glow = "testA.png";

        var anotherSrc = "testB.png";


        function onmouseoverFunc(element) {
            element.src = new_source_for_image_with_glow;
        }

        function onmouseoutFunc(element) {
            element.src = anotherSrc;
        }
    </script>


<a href="#">
    <img id="visit_image" src="new_source_for_image_with_glow" width="350" height="350"
         onmouseover="onmouseoverFunc(this)"
         onmouseout="onmouseoutFunc(this)">
</a>