【问题标题】:Replace the end of a File Name (SRC)替换文件名 (SRC) 的结尾
【发布时间】:2012-05-25 02:26:16
【问题描述】:

我这里有点问题。我在网上找到了这个脚本,并根据我的需要对其进行了一些修改。 此脚本显示鼠标悬停后的图像预览。

最初,只有一种方法可以做到这一点。但是我的网站上有 2 个不同的部分,我想在其中显示具有不同属性(高度、宽度)的图像。 我能够做到这一点,问题是在第二部分,src(文件名)是我想要显示的实际图片的缩略图,所以当它下降时,它只会炸毁一张非常小的图片,看起来很坏。但希望这会使事情变得更容易:所有缩略图都命名为 whaterver_small.jpg 和原件,whaterver_small.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 图片永远不会被截断的地方。

【问题讨论】:

  • 这个脚本看起来像是从 1990 年代的页面中提取的。您最好将 jQuery 作为解决方案的一部分。您可以在几行代码中做到这一点。
  • 我很喜欢,但我是 jquery 的新手
  • 是时候飞跃了。为 IE 编写单独的代码块是疯狂的。
  • 同意。我在网上获得了该代码,完成了这项工作,但你说得对,我应该进入 jquery。我想。 =]

标签: javascript replace filenames src


【解决方案1】:

替换showImage2函数中的以下行

nIMG.setAttribute('src',isImg);

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

【讨论】:

  • 如果您有时间,您认为您可以再看一遍吗?谢谢
  • 如果问题得到解决,您应该接受答案(不需要声誉,请参阅here;声誉仅用于支持投票)。如果您有额外的问题,您应该创建一个新问题并且不要更新旧问题,因为新问题将排在队列的开头,所以很多人可以找到它。更新后的问题只会找到人,他们会随机看到它。
  • 我现在得走了,但我有个想法可以解决你的新问题。如果我有一点时间,我会更新我的答案。
  • 谢谢,我不知道如何接受它,我也不知道我是否应该创建一个新问题,所以谢谢你让我知道。再次感谢@scessor
【解决方案2】:

如果我正确理解您想要的内容,这将从您的所有图像源中删除所有 _small 匹配项:

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

【讨论】:

  • 一个建议:使用 replace 正则表达式 /_small(?=\.)/ 并在末尾提前一段时间,因此它仅在 _small 出现在扩展之前时替换。
  • 非常感谢您的帮助
  • 没问题。如果这个答案对您有帮助,您是否介意接受并可能也赞成它? (通过我答案左侧的复选标记和向上箭头)谢谢! :)
  • 尽快,我的“名声”还是太低了哈哈
猜你喜欢
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 2015-06-06
  • 2022-01-20
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多