我对 JQuery 相当陌生。单击以更改包含占位符的图像以将图像替换为新图像时,我已经能够获得颜色链接。我遇到的问题是,当您单击红木颜色等颜色链接时,顶部的地址栏应显示网页以及单击的颜色参数并保持红木颜色。单击红木图像时地址栏的外观示例:
".../wb/JourneymenSelect.htm?color=redwood"
还有一个按钮,上面写着“电子邮件”,应该能够从地址栏中获取链接,当您单击该链接时,应该显示正确的颜色选择。e.preventDefault(); 如果我删除它,则行,除了单击颜色链接时,您会看到新图像的快速闪烁,但不会保留新图像信息。以下是我正在使用 HTML 部分的代码的主要部分:
<div style="text-align: center;" id="houseImage">
<img src="images/houses/JourneymenSelect/charcoal_gray.jpg" width="320" height="206" class="houseimage" alt="" /></div>
<div id="colorbuttons" style="width: 300px; margin: 0px auto; margin-top: 10px; padding-left: 20px;
text-align: center; font-style: italic;">
<span>Click swatches below to change panel color on house</span><br />
<br />
COLORSCAPES® DARK COLOR<br />
<a href="JourneymenSelect.htm?color=charcoal_gray"" class="myButton" style="background-image: url(images/buttons/charcoal_gray.png)!important;"> </a>
<a href="JourneymenSelect.htm?color=redwood" class="myButton" style="background-image: url(images/buttons/redwood.png)!important;"> </a><br />
<a href="JourneymenSelect.htm?color=natural_cedar" class="myButton" style="background-image: url(images/buttons/natural_cedar.png)!important;"> </a>
<a href="JourneymenSelect.htm?color=heritage_blue" class="myButton" style="background-image: url(images/buttons/heritage_blue.png)!important;"> </a>
<a href="JourneymenSelect.htm?color=shamrock" class="myButton" style="background-image: url(images/buttons/shamrock.png)!important;"> </a><br />
<br />
jQuery代码:
$('#colorbuttons a').click(function (e) {
e.preventDefault();
// get the link and its 'href' attribute...
var linkImage = $(this);
var link = linkImage.attr('href');
var extension = '.jpg';
//alert(link);
// split the 'href' attribute with the '=' character and get the
// last element in the array...
link = link.split('=');
//alert(link);
var filename = link[link.length - 1] + extension;
//alert(filename);
// now we can create the image we're going to put in the
// Replace image container...
var image_folder = 'images/houses/';
//alert(image_folder + filename);
//Get the folder name of where image will be coming from
//This will help when there are muliple pages with different folder locations
var folderPath = $('.houseimage').attr('src');
folderPath = folderPath.split('/');
var folderName = folderPath[folderPath.length - 2] + '/';
// alert(folderName);
var replace_image = $('<img class="houseimage" width="320" height="206" alt="" />');
replace_image.attr('src', image_folder + folderName + filename);
//alert(replace_image.attr('src'));
// set the HTML of the container to the new image...
// first, clear out whatever HTML was in there, then add
// the new image...
$('.houseimage').html('');
// alert($('.houseimage').html() + 'this is empty string');
$('.houseimage').replaceWith(replace_image);
});
我只使用一个页面,因为可能还有其他相关页面,并试图使其易于管理。你能帮忙的话,我会很高兴。