在屏幕上显示选择选项图像

Display Select Option Images On Screen

本文关键字:选项 图像 选择 显示 屏幕      更新时间:2023-09-26

我想在屏幕上显示所有选择选项值。我进行头脑风暴的一种方式如下:

<select size="4">
<option></option>
<option></option>
<option></option>
<option></option>
</select>
我遇到的问题是,是的,

所有内容都已显示,但没有一个是可识别的。我希望当单击其中一个时,它将用户重定向到另一个 URL。我还希望每个选项都是一个横幅。

这样做的原因是因为我已经有 10 张被称为 10 个城市的图像,我想记录用户的城市选择,以便我能够为他们填充正确的邻域。

更新:

以下是其各自图像的链接:

<a href="createEventRestaurant.html""><img id="selectNeighbourhood1" src="content/San-Francisco/berkeleyCampanile.jpg"></a>

<a href="createEventRestaurant.html" target="_self"><img  id="selectNeighbourhood" src="content/San-Francisco/castro.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img  id="selectNeighbourhood"src="content/San-Francisco/dogpatch-tasting-room.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood"src="content/San-Francisco/FD1.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Fishermans-Wharf.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Golden-Gate-Park.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Hayes-Valley.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Marina.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Mid-market.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Mission-district.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Noe-Valley.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Pacific-heights.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Russian-hill.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Soma.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Stanford-university.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Sunset-district.jpg"></a>
<a href="createEventRestaurant.html" target="_self"><img id="selectNeighbourhood" src="content/San-Francisco/Tenderloin.jpg"></a>

让这个成为你的html

<select id="select" size="4">
<option value="http://www.google.com">option</option>
<option>option</option>
<option>option</option>
<option>option</option>
</select>

这是你的JavaScript

var select = document.getElementById('select')
select.addEventListener('click', go, false);
function go (e) {
    e.preventDefault();
    var url = e.target.value;
    window.location.href= url;
}

这将允许您在单击该选项时被重定向

我有一些东西可能对你有用。它展示了如何创建自己的"下拉列表",获取用户单击的内容,并使用适当的图像填充 DIV。我希望这就是你要做的。

jsFiddle 演示

"下拉菜单"由 DIV 创建。有一个外部 DIV,#top 和一些 .menuitem 级的子div。menuitemdiv 包含在一个名为 #menu 的包装器div 中,它可以向上/向下滑动,从而给出<select>控件的外观。

当您单击城市名称时,jQuery 代码会读取单击的选择的 ID,并使用该信息填充picFramediv。如果我要重写这个,我会这样做:

修改后的 jsFiddle - 它不会插入整个 img 标签,而是只更新现有 img 标签的 src= 属性

请注意,jsFiddle 有一些问题...这不是一个完美的环境。首先,#headdiv("选择一个城市")是透明的。不应该。此外,城市名称应该居中 - 但jsFiddle不优先考虑ID(在css中,id应该"赢得"类)

无论如何,这是一个开始。希望对您有所帮助。

.HTML:

<div id="top">
    <div id="head" class="clickable">Choose a City</div>
    <div id="menu">
        <div class="menuitem clickable" id="m1">
            <div class="thepic" id="pic1"><img src="http://placekitten.com/300/50" /></div>
            <div class="moveleft">Cupertino</div>
        </div>
        <div class="menuitem clickable" id="m2">
            <div class="thepic" id="pic2"><img src="http://placekitten.com/300/51" /></div>
            <div class="moveleft">San Francisco</div>
        </div>
        <div class="menuitem clickable" id="m3">
            <div class="thepic" id="pic3"><img src="http://placekitten.com/300/49" /></div>
            <div class="moveleft">Silicon Valley</div>
        </div>
    </div>
</div>
<div id="picframe"></div>

j查询:

var themap;
$('#head').click(function(){
    $('#menu').animate({
        top: 0
    },800);
});
$('.menuitem').click(function(){
    var loc = this.id;
    if (loc=='m1'){
        themap = '<img src="http://placekitten.com/g/150/150" />';
    }else if (loc=='m2'){
        themap = '<img src="http://placekitten.com/149/151" />';
    }else{
        themap = '<img src="http://placekitten.com/g/151/149" />';
    }
    $('#menu').animate({
        top: '-300px'
    },1000);
    $('#picframe').html(themap);
});

.CSS:

img{opacity:.3;}
.clickable{cursor:pointer;}
#head{height:35px;width:304px;font-size:2em;text-align:center;z-index:2;background:white;}
#menu{position:relative;top:-300px;}
.menuitem{position:relative;width:304px;height:54px;margin:5px 0;}
.thepic{position:absolute;top:0;left:0;}
.moveleft{position:relative;top:10px;left:0;background:transparent;font-size:2em;color:blue;}
#pic1{width:130px;margin:0 auto;}
#pic2{width:200px;margin:0 auto;}
#pic3{width:230px;margin:0 auto;}
#picframe{height:150px;width:150px;border:1px solid grey;}

参考(重新 css 优先级):

http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/