1

我要做的是根据页面标题,正文中的图像会改变。例如,如果页面标题是“We Love Apples”,则正文中的图像将是一个苹果。如果标题包含另一个水果,它将显示另一个水果。如果页面标题不包含脚本正在查找的任何单词,则正文中将有一个默认图像。

我确实从http://www.codingforums.com/archive/index.php/t-218600.html找到了这个脚本,但我似乎无法让它工作。

有人有任何提示或指示吗?感谢帮助!

<!doctype html>
<html>
<title>2image</title>
<head>
</head>
<body>

<div><img src="" alt="anImageDesc" name="myimage" width="50px" height="50px" id="someImage" />
</div>

<script type="text/javascript">
var imgs = [
"http://www.domain.com/images/image_255 Company_Name.jpg",
"http://www.domain.com/images/image_256 Company_Name.jpg",
"http://www.domain.com/images/image_257 Company_Name.jpg" //no comma after last image
];
var el = document.getElementById("someImage");
var title = document.title;
for (var i=0;i<imgs.length;i++){
if (imgs[i].indexOf(title) != -1) { 
el.src = imgs[i];
break;  
} else {el.src = "image_256.gif"} //a default image incase nothing is found
}
</script>

</body>
</html>
4

4 回答 4

1

看到这个: DEMO

我建议在标题中查找令牌的键/值映射,并将适当的图像与该令牌相关联:

var images = {
    'very large apple' : 'http://www.domain.com/images/very_large_apple_image_here.jpg',
    'large apple' : 'http://www.domain.com/images/large_apple_image_here.jpg',
    'apple' : 'http://www.domain.com/images/apple_image_here.jpg',
    'orange' : 'http://www.domain.com/images/orange_image_here.jpg'
};

var defaultImage = images['apple']; //Set your default here.
var imageElement = document.getElementById("someImage");
var title = document.title.toLowerCase();
var source = null;

for (var t in images) {
    if (title.indexOf(t.toLowerCase()) != -1) {
        source = images[t]; //Found a match.
        break;
    }
}

if (!source) source = defaultImage;

imageElement.src = source;
于 2013-09-10T18:17:52.943 回答
0

用于document.title获取窗口标题,并据此设置您的图像。我过去常常toUpperCase()确保您找到您正在寻找的文本,无论大小写。search()如果未找到文本,将返回 -1。

if(window.title.toUpperCase().search('APPLE') > 0)
{
    //change image to apple
}
else if(document.title.toUpperCase().search('ORANGE') > 0)
{
    //change image to orange
}
else 
{
    //use the default image
}
于 2013-09-10T18:13:29.400 回答
0

定义要查找的水果,然后检查标题以查看它是否包含任何水果,然后找到合适的图像:

var fruits = [
    'apple',
    'orange',
    'grape'
]

var imgs = [
    "http://www.domain.com/images/image_255_grape.jpg",
    "http://www.domain.com/images/image_256_orange.jpg",
    "http://www.domain.com/images/image_257_apple.jpg"
];

var fruit = 'apple', // default
    img   = '';

// check the title for fruit
for (var i=0; i<fruits.length; i++) {}
    if ( document.title.indexOf(fruits[i]) != -1 ) fruit = fruits[i];
}

//find matching image
for (var i=0; i<imgs.length; i++) {}
    if ( imgs.indexOf(fruit) != -1 ) img = imgs[i];
}

var el = document.getElementById("someImage");
el.src = img;
于 2013-09-10T18:20:20.083 回答
0

创建查找:

var myList = {
    images: [{
        id: "grape",
        name: "somelocation/file.png"
    }, {
        id: "crabapple",
        name: "c:/images/brightblue.png"
    }, {
        id: "lime",
        name: "grassgreen.png"
    }, {
        id: "bannana",
        name: "dollarbill.png"
    }]
};

var lookup = {};
// create refernece to list above and use it everywhere
lookup.list = myList;
for (var i = 0, len = lookup.list.images.length; i < len; i++) {
    lookup[lookup.list.images[i].id] = lookup.list.images[i];
}
alert(lookup['lime'].name);

和标题:

lookup[document.title].name;
于 2013-09-10T18:27:26.143 回答