简体   繁体   English

使用javascript动态链接html页面

[英]Dynamically linking html pages with javascript

This is the code to store five images for an HTML page and then the function is used to display those five images one at a time. 这是为HTML页面存储五个图像的代码,然后该功能用于一次显示这五个图像。

This code works and one can scroll through the five images. 此代码有效,可以滚动五个图像。

My question is whether it is possible to direct to different HTML page on the basis of image shown at a particular time 我的问题是,是否可以根据特定时间显示的图像指向不同的HTML页面

<script type="text/javascript">
    var curImage = 0;
    var cImages = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg'];

    function townHousePics(direction) {
        if (direction == 'next') {
            curImage++;
            if (curImage == 5) {
                curImage = 0;
            }        
        } 
        else 
        {
            curImage--;
            // even reset the counter here when
            // its getting 0
        }
        document.images[0].src = cImages[curImage];
    }
</script>

html setup HTML设置

<a href='' id='my-anchor-name'><img id='my-image-name' src=''/></a>

javascript JavaScript的

<script type="text/javascript">
    var curImage = 0;
    var cImages = [{aHref:'target1.html',imgHref:'1.jpg'},{aHref:'target2.html',imgHref:'2.jpg'},
{aHref:'target3.html',imgHref:'3.jpg'},
{aHref:'target4.html',imgHref:'4.jpg'},
{aHref:'target5.html',imgHref:'5.jpg'}
];
         function townHousePics(direction) {
                     if (direction == 'next') {
                         curImage = (curImage++)%cImages.length ;
                      else {
                         curimage = (curImage--)%cImages.length;
                     }
                     var img = document.getElementById('my-image-name');
                     var anchor = document.getElementById('my-anchor-name')
                     img.src = cImages[curImage].imgHref;
                     anchor.href = cImage[curImage].aHref;
             }
</script>

Automatic file-name-based Replacement 基于文件名的自动替换

Suppose you had a link like the following: 假设您有如下链接:

<a href="1.html" id="viewLarge">View Large</a>

That you wanted to dynamically change with each image. 您希望随每个图像动态更改。 When image 2.jpg becomes the primary, we swap out 1.html for 2.html. 当图像2.jpg成为主要内容时,我们将1.html换成2.html。 We could modify the last line of your code to this: 我们可以将代码的最后一行修改为:

// Updated Source of First Image
document.images[0].src = cImages[curImage];
// Parse Image Number from String
var currentNumber = parseInt( curImage, 10 ); // 2.jpg becomes 2
// Get a reference to our 'viewLarge' link
var ourLink = document.getElementById("viewLarge");
// 1.html becomes 2.html when 2 is the currentNumber;
ourLink.href = ourLink.href.replace( /^([0-9]+)/, currentNumber );

Note, though this works it restricts you to having a 1.html for your 1.jpg . 请注意,虽然这有效,但它限制了你的1.jpg1.html This may be undesirable. 这可能是不合需要的。 You may want 1.jpg to go with CityLoft.html . 您可能希望1.jpgCityLoft.html一起使用。 If that's the case, you'd want to associate the image with the proper url in your cImages object: 如果是这种情况,您需要将图像与cImages对象中的正确URL相关联:

Use Objects to store Related Data 使用对象存储相关数据

var cImages = [{image: '1.jpg', page: 'cityLoft.html'},
               {image: '2.jpg', page: 'townhome.html'}];

Once you've made this change, you only need to restore the references in your code. 完成此更改后,您只需要恢复代码中的引用。 For instance, we would change the following: 例如,我们将更改以下内容:

document.images[0].src = cImages[curImage];

To this: 对此:

document.images[0].src = cImages[curImage].image;

We would also need to update our code that links to the relevant page as well: 我们还需要更新链接到相关页面的代码:

document.getElementById("viewLarge").href = cImages[curImage].page;

That's it! 而已!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM