getElementsByName not working
getElementsByName not working
查找了这个问题的几个"答案",但它主要是人们不把getElementsByName()返回的结果作为一个NodeList!
编辑:我试图隐藏/显示基于被点击的元素元素。我可以硬编码这个using文档。getElementById,每次我有想要隐藏/显示的元素时添加一个。但这将是理想的,如果我可以检索所有元素命名的东西,只是在他们上运行一个循环来隐藏/显示。然后我可以在编写时给元素加上一个名称,这样循环就可以正常工作而不需要修改。下面我的代码只是试图弹出一个警报值用于测试目的。到目前为止,它一直以null错误中断。我正在使用和设计ie9,因为这是公司使用的。
代码: <input type="radio" name="Area" value="Engineering" id="EngineeringCheck" onclick="javascript: ShowContentEngineering();" />Engineering
<script type="text/javascript">
function ShowContentEngineering() {
alert(document.getElementsByName('EngineeringAreas')[0].value)
document.getElementById('InformationBlock').style.display = 'block';
}
</script>
<h5 name="EngineeringAreas" value="luls"> WHAT THE HECK </h5>
上面的代码说对象在getElementsByName('EngineeringAreas')[0]是空的。显然,在它下面,它不是空的…我混淆了getElementsByName('string')[0]吗?值与元素的值?还是在检索其他值?
理想情况下,我稍后会添加其他元素,用"EngineeringAreas"标记它们,并且永远不必与隐藏/显示功能混淆。
编辑:下面是错误信息:
Unhandled exception at line 53, column 9 in http://localhost:57264/Home/Index
0x800a138f - Microsoft JScript runtime error: Unable to get value of the property 'value': object is null or undefined
给你…似乎是:
-
onclick="javascript:
<——不需要——只引用函数名 -
ShowContentEngineering
需要在窗口上下文中设置 - 你正在引用一个不允许值属性的元素的"value"属性(h5)
我让它工作,而不是抓取h5的innerHTML代码
<input type="radio" name="Area" value="Engineering" id="EngineeringCheck" onclick="ShowContentEngineering();" />Engineering
<h5 name="EngineeringAreas"> WHAT THE HECK </h5>
<script>
window.ShowContentEngineering = function() {
alert(document.getElementsByName('EngineeringAreas')[0].innerHTML)
document.getElementById('InformationBlock').style.display = 'block';
}
</script>
这是一个工作小提琴:https://jsfiddle.net/mu970a8k/
在.getElementsByName('EngineeringAreas')
和.value
之间插入.attributes[1]
。1指向名为EngineeringAreas的<h5>
元素中的第二个属性,即value
。将.value
放在.attributes[1]
之后,应该会在警告框中返回值文本"luls"。警报代码应该这样设置:
alert(document.getElementsByName('EngineeringAreas')[0].attributes[1].value);
更多信息: http://www.w3schools.com/jsref/prop_attr_value.asp
- $rootScope not working
- jQuery document.ready not working
- Javascript getElementsByTagName not working?
- JQuery.val( ) not working
- Tomcat websocket is not working
- Javascript JSON.parse not working
- NodeJS Multer is not working
- Regex not working
- Ember run.debounce not working
- AngularJs 1 not working
- document.getElementById.style.backgroundImage not working
- Rxjs5, distinct() is not working
- createOscillator noteOn not working
- Javascript - .getHours() not working
- Javascript setTimeout not working | onkeydown
- MongoDB $pull not working
- Express router.use not working
- JSONP not working
- window.opener not working in chrome & IE
- Backbone + RequireJS: Collection.each() is not working