Javascript 帮助 关于 “document.getElementById” 请
Javascript Help about "document.getElementById" please
这是我的代码:
<html>
<head>
<script>
function changeDate(option) {
var selectList = document.getElementById("catProdAttributeItem");
if (option == 0) {
selectList.selectedIndex++;
} else if (option == 1 && selectList.selectedIndex > 0) {
selectList.selectedIndex--;
}
}
</script>
</head>
<body>
<img src="img1.jpg" onclick="changeDate(0)">
<img src="img2.jpg" onclick="changeDate(1)">
<select id="pa_colour" name="attribute_pa_colour">
<option value="">Choose an option…</option>
<option value="black" class="active">Black</option>
<option value="blue" class="active">Blue</option>
<option value="red" class="active">Red</option>
<option value="white" class="active">White</option>
</select>
<div id="catProdAttributeItem">
<select>
<option id="1">One</option>
<option id="2">Two</option>
<option id="3">three</option>
<option id="4">Four</option>
</select>
</div>
</body>
</html>
在此代码中,我要做的是当用户单击图像时,选择选项值将更改。
如果选择<select id="list">
有一个id,它将正常工作,但就我而言,选择选项没有id,但在选择之前有一个类"catProdAttributeItem"
。如何使其与 document.getElementById 一起使用并选择选项?
Select 元素
是 Div 的子元素,因此您需要访问子元素。为此,请使用以下内容:
var selectList = document.getElementById("catProdAttributeItem").children[0];
这是工作演示: http://jsfiddle.net/spdX3/
由于 select 元素位于 catProdAttributeItemdiv 中,因此可以使用 childNodes 属性选择它
var selectList = document.getElementById("catProdAttributeItem").childNodes[0];
其余代码应按原样工作。
相关文章:
- reactjs this.refs vs document.getElementById
- 如何通过adf中的document.getElementById获取inputText字段值
- 无法获取文档.GetElementById工作正常
- document.getElementById(“st”).click();不起作用
- Javascript:Can't使用getElementById获取元素
- 如何使用“;getElementById”;在一个循环中
- Javascript GetElementByID has no value
- document.getElementById并使用id名称
- document.getElementById在js中不起作用
- document.getElementById.style.backgroundImage not working
- 如何从document.getElementById()中获取UniqueID
- 从输入框、Javascript、getElementById获取值
- 什么'这个javascript代码getElementById有问题
- DOMDocument getelementbyid conflict?
- 如何从getelementbyid用javascript编写变量
- inline svg--getElementById在Opera中不起作用
- 在init function()中调用getElementById(“..”),而不是想用它做点什么的函数
- 将global.getelementbyid连接到单个变量中
- 未捕获的类型错误:未定义不是上的函数.GetElementByID
- document.getElementById(..) is null