id属性返回null或undefined

时间:2015-05-29 10:04:51

标签: javascript html html5

我试图将get属性挂起getElementById方法,但它似乎返回null或undefined。我检查了我的HTML,它确实有一个与之关联的ID。我已经为我的所有列提供了不同的ID,因此多次出现相同ID并不是问题。

var columnID = $(this).closest(".col-md-4.column").id;
console.log(columnID);
var column = document.getElementById(columnID);

 <div class="row" id="columns">
<div class="col-md-4 column" id="col1">
    <div class="panel panel-default" draggable="true">
        <div class="panel-heading">
            <h3 class="panel-title">
                Panel title
                <span class="glyphicon glyphicon-pencil panel-icons"></span>
                <span class="glyphicon glyphicon-zoom-in panel-icons"></span>
                <span class="glyphicon glyphicon-trash panel-icons"></span>
            </h3>
        </div>
        <div class="panel-body" id="testchart">
            CHART GOES HERE
        </div>
    </div>

    <div class="panel panel-default" draggable="true">
        <div class="panel-heading">
            <h3 class="panel-title">
                Panel title
                <span class="glyphicon glyphicon-pencil panel-icons"></span>
                <span class="glyphicon glyphicon-zoom-in panel-icons"></span>
                <span class="glyphicon glyphicon-trash panel-icons"></span>
            </h3>
        </div>
        <div class="panel-body">
            World
        </div>
    </div>

    <div class="panel panel-default" draggable="true">
        <div class="panel-heading">
            <h3 class="panel-title">
                Panel title
                <span class="glyphicon glyphicon-pencil panel-icons"></span>
                <span class="glyphicon glyphicon-zoom-in panel-icons"></span>
                <span class="glyphicon glyphicon-trash panel-icons"></span>
            </h3>
        </div>
        <div class="panel-body">
            Hello
        </div>
    </div>
</div>
<div class="col-md-4 column" id="col2">

</div>
<div class="col-md-4 column" id="col3">

3 个答案:

答案 0 :(得分:4)

试试这个:

var columnID = $(this).closest(".col-md-4.column").attr('id');

答案 1 :(得分:1)

没有id属性。使用.attr('id')

答案 2 :(得分:0)

尝试

$(this).closest(".col-md-4.column").attr('id')

必须使用.attr方法

访问属性