替换文件名(SRC)的结尾

时间:2012-05-17 13:45:28

标签: javascript replace filenames src

我在这里遇到了一些问题。我在网上找到了这个脚本,并根据我的需要改变了一下。 此脚本显示鼠标onmouseover后的图像预览。

最初,只有一种方法可以做到这一点。但我在我的网站上有两个不同的部分,我想在其中显示具有不同属性(高度,宽度)的图像。 我能够做到这一点,问题是在第二部分,src(文件名)是我想要显示的实际图片的缩略图,所以当它显示时,它只会炸掉一张非常小的图片,看起来很像很坏。但希望这会让事情变得更容易:所有的缩略图都被命名为whaterver_small.jpg和原文,whatever.jpg现在,如果我可以删除_small或_small.jpg并从该文件的末尾替换为.jpg,那将显示这张照片对我来说很棒。这是在html上调用函数的方式:

原始尺寸,无需更改:

<a href=http://www.whatever.net/1.html');">
<img alt="Copper" border="1" height="64" src="http://www.whatever.net/whatever_small.jpg" width="85" onmouseover="showImage1(this.src,this,'Whatever')" /></a>

显示image2,我遇到问题的那个。

<a href=http://www.whatever.net/1.html');">
<img alt="Copper" border="1" height="64" src="http://www.whatever.net/whatever_small.jpg" width="85" onmouseover="showImage2(this.src,this,'Whatever')" /></a>

这是脚本

    var floatWidth = 150;  // set the width of the floating image
    var floatHeight = 100;  // set its height
    var floatWidth2 = 320;  // set the width of the floating image
    var floatHeight2 = 240;  // set its height

    var midWindow = 0;
    var nContainer = "";
    var IE = false;

    if (navigator.appName == 'Microsoft Internet Explorer'){IE = true}

    function stayHome(m){        

            if (IE)
                    {
                    var currX = event.clientX;
                    var currY = event.clientY;
                    }
            else        {
                    var currX = m.pageX;
                    var currY = m.pageY;
                    }
            if (document.documentElement && document.documentElement.scrollLeft || document.documentElement && document.documentElement.scrollTop)
                    {
                    var iL = document.documentElement.scrollLeft;        
                    var iV = document.documentElement.scrollTop;
                    }
            else        {
                    var iL = document.body.scrollLeft;        
                    var iV = document.body.scrollTop;        
                    }
            if (currX > midWindow+80)
                    {
                    var msgWidth = nContainer.clientWidth;
                    if (IE){nContainer.style.left = (currX-msgWidth-10+iL)+'px'}
                    else {nContainer.style.left = (currX-msgWidth-10)+'px'}
                    }
            else        {
                    if (IE){nContainer.style.left = (currX+15+iL)+'px'}
                    else {nContainer.style.left = (currX+15)+'px'}
                    }
            if (IE){nContainer.style.top = (currY+iV-(floatHeight/2)+70)+'px'}
            else {nContainer.style.top = (currY-(floatHeight/2)+70)+'px'}
    }        

    function hideImage(){

            while (nContainer.lastChild)
                    {nContainer.removeChild(nContainer.lastChild)}
            document.getElementById('isFloat').style.display = 'none';
    }

    function showImage(isImg,currItem,currCaption){

            document.getElementById('isFloat').style.display = 'inline';
            nIMG  = document.createElement('img');
            nContainer.appendChild(nIMG);
            nIMG.setAttribute('src',isImg);
            nIMG.setAttribute('width',floatWidth);
            nIMG.setAttribute('height',floatHeight);
            nCaption = document.createElement('div');
            nCaption.style.textAlign = "center";
            nCaption.style.backgroundColor = '#EAE3C6';
            nCaption.style.padding = '5px';
            nCaption.style.color = '#000000';
            nCaption.style.fontFamily = 'Sans-serif';
            nCaption.style.fontSize = '10pt';
            nCaption.style.borderTop = "1px solid black";
            nContainer.appendChild(nCaption);
            nCaption.innerHTML = currCaption;
            currItem.onmouseout=hideImage;
    }

    function showImage2(isImg,currItem,currCaption){

            document.getElementById('isFloat').style.display = 'inline';
            nIMG  = document.createElement('img');
            nContainer.appendChild(nIMG);
            nIMG.setAttribute('src',isImg);
            nIMG.setAttribute('width',floatWidth2);
            nIMG.setAttribute('height',floatHeight2);
            nCaption = document.createElement('div');
            nCaption.style.textAlign = "center";
            nCaption.style.backgroundColor = '#EAE3C6';
            nCaption.style.padding = '5px';
            nCaption.style.color = '#000000';
            nCaption.style.fontFamily = 'Sans-serif';
            nCaption.style.fontSize = '10pt';
            nCaption.style.borderTop = "1px solid black";
            nContainer.appendChild(nCaption);
            nCaption.innerHTML = currCaption;
            currItem.onmouseout=hideImage;
    }


    function getMidWindow(){

            if (document.documentElement && document.documentElement.scrollLeft || document.documentElement && document.documentElement.scrollTop)
                    {
                    midWindow = document.documentElement.clientWidth/2;
                    }
            else        {
                    midWindow = document.body.clientWidth/2;
                    }
    }

    function initFloatImg(){

            var nBody = document.getElementsByTagName('body')[0];
            var nDiv = document.createElement('div');
            nDiv.id = "isFloat";
            nDiv.style.position = "absolute";
            nDiv.style.top = "0px";
            nDiv.style.left = "0px";
            nDiv.style.border = "1px solid black";
            nDiv.style.padding = "5px";
            nDiv.style.backgroundColor = "#ffffff"
            nBody.appendChild(nDiv);
            nContainer = document.getElementById('isFloat');
            document.onmousemove = stayHome;
            hideImage();
            if (!IE){document.captureEvents(Event.mousemove)}
            getMidWindow();
    }

    onload=initFloatImg;
    onresize=getMidWindow;

更新

好的,所以我更新了这个页面中的脚本,现在它运行得很好。 我遇到了另一个问题,当鼠标悬停的图片接近页面末尾时,预览会被截止。我希望能够向上移动预览,所以没有滚动条。 以下是功能实例的实例:http://www.soccer.com/Navigation.process?Ne=178&N=4294960224+346 图片永远不会截止。

2 个答案:

答案 0 :(得分:0)

如果我理解你想要的内容,这将删除所有图片来源的所有_small个匹配项:

$("img").each(function() {
    $(this).attr("src", $(this).attr("src").replace("/_small(?=\.)/", ""));
});

答案 1 :(得分:0)

替换showImage2功能

中的以下行
nIMG.setAttribute('src',isImg);

nIMG.setAttribute('src',isImg.replace(/_small\./, '.'));