-4

我需要帮助。如何在下面的代码中添加指向图像的链接。这是一个脚本,允许访问者单击表单中下拉菜单上的选项,每个选项都会更改呈现的图像。我正在尝试为每个图像添加一个链接,允许访问者将鼠标悬停在图像上并打开一个更大的图像。

我只需要知道在哪里添加链接。每次我在 var sel_imgs=[ 中添加它时,它都会在访问者在下拉菜单中选择一个选项时禁用更改图像的功能。

function diva_imgBySel(objId,theValue) { //v0.1 divaHTML.com

    var sel_imgs=["images/md01.jpeg","images/md01.jpeg","images/md02.jpeg","images/md03.jpeg","images/md04.jpeg","images/md06.jpeg","images/md05.jpeg","images/pic07.jpeg","images/pic08.jpeg"];

    var d=document;
    theValue=sel_imgs[theValue];
    if (!theValue || !d.getElementById ) return;
    var obj = d.getElementById(objId);
    if (obj) obj.src=theValue;    
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
</script>

这是页面下方的表单代码:

<img src="images/md01.jpeg" alt="" name="theImg" width="147" height="161" id="theImg" style="border: 1px solid #ffffff;" align="LEFT" /><strong>Add a vase:</strong>
<input type="checkbox" name="anniv" id="anniv" value="6.95" onclick="priceUpdate('anniv');"/>
             &pound;6.95
            </td>
          <td width="71%" align="left" valign="top"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td align="left"></br>&nbsp;&nbsp;&nbsp;&nbsp;<select name="productColors" id="productColors" style="width:270px; margin-bottom:5px; margin-top:5px" onchange="diva_imgBySel('theImg',this.selectedIndex)">
                <option value="MD1004">Select Flower Arrangement</option>
                <option value="MD1004">Bloom</option>
                <option value="MD1001">Majestic</option>
                <option value="MD1007">Mothers Day Premium</option>
                <option value="MD1002">Mum's the Word</option>
                <option value="NF1011">Joyful Orchids</option>
                <option value="NF1010">Lily and Rose Hand Tied</option>
                <option value="NF1013">Sunflower Sensation</option>
                <option value="SF002-free-vase">Tranquililty</option>
                </select></td>

这是附加了鼠标悬停效果的节点。它工作正常,但为用户所做的每个选择重复相同的大图像。我需要知道如何为每个图像复制这种效果,而不仅仅是“md01.jpeg”。

<a href="#" rel="imgtip[1]"><img src="images/md01.jpeg" alt="" name="theImg" width="147" height="161" id="theImg" style="border: 1px solid #ffffff;" align="LEFT" /><a/>

<link rel="stylesheet" type="text/css" href="ddimgtooltip.css" />

<script type="text/javascript" src="ddimgtooltip.js">

/***********************************************
* Image w/ description tooltip v2.0- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more
***********************************************/

</script>

这是图像鼠标悬停的代码。它是一个 .js 文件

/* Image w/ description tooltip v2.0
* Created: April 23rd, 2010. This notice must stay intact for usage 
* Author: Dynamic Drive at http://www.dynamicdrive.com/
* Visit http://www.dynamicdrive.com/ for full source code
*/


var ddimgtooltip={

    tiparray:function(){
        var tooltips=[]
        //define each tooltip below: tooltip[inc]=['path_to_image', 'optional desc', optional_CSS_object]
        //For desc parameter, backslash any special characters inside your text such as apotrophes ('). Example: "I\'m the king of the world"
        //For CSS object, follow the syntax: {property1:"cssvalue1", property2:"cssvalue2", etc}

        tooltips[0]=["images/pic01.jpg"]
        tooltips[1]=["images/pic02.jpg"]
        tooltips[2]=["images/pic03.jpg"]
        tooltips[3]=["images/pic04.jpeg"]
        tooltips[4]=["images/pic05.jpeg"]
        tooltips[5]=["images/pic06.jpeg"]

        return tooltips //do not remove/change this line
    }(),

    tooltipoffsets: [20, -30], //additional x and y offset from mouse cursor for tooltips

    //***** NO NEED TO EDIT BEYOND HERE

    tipprefix: 'imgtip', //tooltip ID prefixes

    createtip:function($, tipid, tipinfo){
        if ($('#'+tipid).length==0){ //if this tooltip doesn't exist yet
            return $('<div id="' + tipid + '" class="ddimgtooltip" />').html(
                '<div style="text-align:center"><img src="' + tipinfo[0] + '" /></div>'
                + ((tipinfo[1])? '<div style="text-align:left; margin-top:5px">'+tipinfo[1]+'</div>' : '')
                )
            .css(tipinfo[2] || {})
            .appendTo(document.body)
        }
        return null
    },

    positiontooltip:function($, $tooltip, e){
        var x=e.pageX+this.tooltipoffsets[0], y=e.pageY+this.tooltipoffsets[1]
        var tipw=$tooltip.outerWidth(), tiph=$tooltip.outerHeight(), 
        x=(x+tipw>$(document).scrollLeft()+$(window).width())? x-tipw-(ddimgtooltip.tooltipoffsets[0]*2) : x
        y=(y+tiph>$(document).scrollTop()+$(window).height())? $(document).scrollTop()+$(window).height()-tiph-10 : y
        $tooltip.css({left:x, top:y})
    },

    showbox:function($, $tooltip, e){
        $tooltip.show()
        this.positiontooltip($, $tooltip, e)
    },

    hidebox:function($, $tooltip){
        $tooltip.hide()
    },


    init:function(targetselector){
        jQuery(document).ready(function($){
            var tiparray=ddimgtooltip.tiparray
            var $targets=$(targetselector)
            if ($targets.length==0)
                return
            var tipids=[]
            $targets.each(function(){
                var $target=$(this)
                $target.attr('rel').match(/\[(\d+)\]/) //match d of attribute rel="imgtip[d]"
                var tipsuffix=parseInt(RegExp.$1) //get d as integer
                var tipid=this._tipid=ddimgtooltip.tipprefix+tipsuffix //construct this tip's ID value and remember it
                var $tooltip=ddimgtooltip.createtip($, tipid, tiparray[tipsuffix])
                $target.mouseenter(function(e){
                    var $tooltip=$("#"+this._tipid)
                    ddimgtooltip.showbox($, $tooltip, e)
                })
                $target.mouseleave(function(e){
                    var $tooltip=$("#"+this._tipid)
                    ddimgtooltip.hidebox($, $tooltip)
                })
                $target.mousemove(function(e){
                    var $tooltip=$("#"+this._tipid)
                    ddimgtooltip.positiontooltip($, $tooltip, e)
                })
                if ($tooltip){ //add mouseenter to this tooltip (only if event hasn't already been added)
                    $tooltip.mouseenter(function(){
                        ddimgtooltip.hidebox($, $(this))
                    })
                }
            })

        }) //end dom ready
    }
}

//ddimgtooltip.init("targetElementSelector")
ddimgtooltip.init("*[rel^=imgtip]")
4

2 回答 2

0

此代码示例实际上并未引用您要关注的标记。您在这里得到的是逻辑和表单元素将更改 id 为“theImg”的 img 标签的 src。您需要查看的是具有 'theImg' id 的元素,并在那里添加一个侦听器以触发您想要的任何大图像效果。

于 2012-07-12T16:54:40.927 回答
0
<option value="MD1001"><a href="your_link"><img src="your_image_path"/></a>Your non-Link Text</option>

我就是这样布置的。我不确定你到底要什么。但这会将图像放在那里。只需将 "your_link" 、 "your_image_path" 替换为您的数据。

于 2012-07-12T17:00:37.030 回答