ParentNode.getElementById() 不起作用
我在练习html的时候遇到了一个问题。当我在 JavaScript 中使用 parentNode
时,我认为这并不难处理。
但是使用 getElementById
或其他函数获取 parentNode
下的某些元素并不符合我的想法。
var this_question = selectObj.parentNode.parentNode;
alert(this_question); //it is working perfectly
alert(this_question.getElementById('question'))
它不起作用。我无法理解...
<script>
function addQuestion(selectObj) {
var this_question = selectObj.parentNode.parentNode;
alert(this_question); //it is working perfectly
alert(this_question.getElementById('question')) //It's not working. I can't understand..
}
</script>
<ol id="question_list">
<li>
<textarea class="question" name="question" id="question"></textarea>
<select name="question_type" id="question_type" onChange="javascript:selectEvent(this)">
<option>-----</option>
<option value="text" >단답형</option>
<option value="paragraph" >서술형</option>
<option value="multiple_choice">다지선</option>
<option value="checkbox">다중선택</option>
<option value="scale">scale</option>
</select>
<div id='answer_div'><p>부가설명:<input name='top_label' id='top_label' type='paragraph' /></p> <p>답변:<input name='answer_text' id='answer_text' type='text' /></p></div>
<p>
<input type="button" value="Add Question" onclick="javascript:addQuestion(this)"/>
<input type="button" value="Done" onclick="javascript:finish()"/>
</p>
</li>
</ol>
I encountered a problem while practicing html. When I used parentNode
in JavaScript, I thought it is not hard to treat.
But to get some element under parentNode
using getElementById
or another function is not working as my thinking.
var this_question = selectObj.parentNode.parentNode;
alert(this_question); //it is working perfectly
alert(this_question.getElementById('question'))
It's not working. I can't understand...
<script>
function addQuestion(selectObj) {
var this_question = selectObj.parentNode.parentNode;
alert(this_question); //it is working perfectly
alert(this_question.getElementById('question')) //It's not working. I can't understand..
}
</script>
<ol id="question_list">
<li>
<textarea class="question" name="question" id="question"></textarea>
<select name="question_type" id="question_type" onChange="javascript:selectEvent(this)">
<option>-----</option>
<option value="text" >단답형</option>
<option value="paragraph" >서술형</option>
<option value="multiple_choice">다지선</option>
<option value="checkbox">다중선택</option>
<option value="scale">scale</option>
</select>
<div id='answer_div'><p>부가설명:<input name='top_label' id='top_label' type='paragraph' /></p> <p>답변:<input name='answer_text' id='answer_text' type='text' /></p></div>
<p>
<input type="button" value="Add Question" onclick="javascript:addQuestion(this)"/>
<input type="button" value="Done" onclick="javascript:finish()"/>
</p>
</li>
</ol>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getElementById() 是文档的方法,在元素中不可用。
您可以使用:
getElementsByTagName() 在 elements 中可用。
getElementById() is a method of documents, not available in elements.
You may use:
getElementsByTagName() is available in elements.
您有两个具有相同
id
属性的元素,但id
属性必须是唯一的:当您重复
id
属性时,会发生奇怪的事情。例如,如果您将来自 HTML4 规范:
以及 HTML5 规范:
You have two elements with the same
id
attribute butid
attributes must be unique:<ol id="question">
<textarea class="question" name="question" id="question"></textarea>
When you duplicate
id
attributes strange things happen. If you change the<textarea>
to haveid="question_text"
, for example, things start working better:From the HTML4 specification:
and from the HTML5 specification:
您可以使用:
它返回:
请参阅:
https://developer.mozilla.org/en- US/docs/Web/API/Element/querySelector
You can use:
It returns:
See:
https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector