【问题标题】:Attaching values to Options like Width and Height将值附加到宽度和高度等选项
【发布时间】:2018-03-27 10:40:32
【问题描述】:

我有选择菜单。每个选项切换显示的图像。我在问如何将链接到每个选项的每个唯一宽度和高度转换为实际显示的图像。我知道宽度和高度对选项标签的影响是没有根据的,但我想我说的是我想要一个关联并绑定到每个选项的值,这些选项在附加选项时起作用并影响图像to 被选中。像添加一个变量之类的东西 var c = a.getElementByTagName('class').getAttributeNode('width').value;??

我真的不知道。如何将值与选项相关联?最好有一种方法,无需将新类附加到每个选项。到目前为止我的代码只能更改src。

我希望你能理解这个问题。我尽我所能解释了。谢谢你的帮助。

<!DOCTYPE html>
<html>
<body>

<select id='select1'>
    <option value='pic_unavail' selected width='300' height='300'>-- Choose --</option>
    <outgroup label="Animals">
    <option value='pic_monkey' id='monkey' width='50' height='100'>Monkey</option>
    <option value="pic_cat" id='cat' width='100' height='200'>Cat</option>
    <option value="pic_dog" id='dog' width='200' height='250'>Dog</option>
    </outgroup>
</select>  

<img id="pic">   

<script type="text/javascript">  

document.getElementById("select1").onchange = select;

var a = document.getElementById('select1')    
var b = document.getElementById('pic');


function select() {
    b.src = this.value;
     return 
}    

 </script>
 </body>
</html>

【问题讨论】:

    标签: image select attributes option


    【解决方案1】:

    用途:

    this.options[this.selectedIndex].getAttribute('width');
    this.options[this.selectedIndex].getAttribute('height');
    

    这应该为您提供所选选项的宽度和高度,就像您当前将它们嵌入每个标签中一样。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2016-08-02
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      相关资源
      最近更新 更多