开发者

How to replace src with regular expression and jquery

开发者 https://www.devze.com 2023-03-04 02:53 出处:网络
I have the following output. <img width=\'70\' height=\'70\' class=\'centreimg\' src=\"http://localhost/aktivfitness_new/assets开发者_如何学编程/images/centre/centre1.jpg\" />

I have the following output.

<img width='70' height='70' class='centreimg' src="http://localhost/aktivfitness_new/assets开发者_如何学编程/images/centre/centre1.jpg" />

There are same outputs with centre2.jpg etc. Now I want to replace this centre1.jpg to hover.jpg when I hover.

But when I use the following it becomes centre1hover.jpg

 $(".centreimg").mouseover(function() {
        var hoverimg = $(this).attr("src").match(/[^\.]+/) + "hover.jpg";
        ...

Something is wrong with match(/[^.]+/) + "hover.jpg part. How can I do this?

Thanks in advance.


Don't you think this would be easier:

var newSrc = oldSrc.replace(/[^\/\.]+\./, 'hover.');

Anyway: you shouldn't use Javascript for hovers =) If there is another way: use it. Not only is it a bad practice, it's also not user friendly: when the image loads when you hover, the user will see a 'flash' because the entire image still has to be loaded = not pretty.

0

精彩评论

暂无评论...
验证码 换一张
取 消