javascript - parentNode.getElementById() 不工作

标签 javascript html dom

我在练习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>

最佳答案

getElementById()是文档的方法,元素中没有。

您可以使用:

this_question.getElementsByTagName('textarea')[0]

getElementsByTagName() 在元素中可用。

关于javascript - parentNode.getElementById() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7625165/

相关文章:

javascript - 如何让ClientScript.RegisterClientScriptBlock返回false而不进入函数

javascript - 使用 RequireJS 和 JSDoc3 记录 Marionette 模块

javascript - dimple js 使用dimple.filterData()后没有返回数据怎么办

html - 为什么我的过渡不适用于 <hr>?

c++ - HTML 文件 : add annotations through IHTMLDocument

javascript - JavaScript 中 JSON.parse() 的复杂性是什么?

javascript - 为 ContentTools 自定义编辑按钮

html - 高度为自动时不显示背景图像

javascript - 包含 javascript 的附加 HTML 无法运行,我怎样才能让它运行?

PHP 获取 DOMNode 的 XPath