this.element不访问元素中的任何数据

时间:2017-02-26 11:36:13

标签: javascript jquery html

我正在尝试动态查找用户点击的元素。当用户点击某个元素时,我希望能够访问该元素的数据。这是我的代码:

JS:

  $('.elements').on('click', function(){

    var elementClicked =   $(this.element).attr('rel'); 

    alert(elementClicked);
    console.log(elementClicked);

});

警报给我一条未定义的消息。

html:

    <body>
    <div id="gameholder">
        <div id="title"></div>
        <div class = "elements">
        <div rel = " first" class="penguin1"></div>
        <div rel = " second" class="penguin2"></div>
        <div rel = " third" class="penguin3"></div>
        <div rel = " fourth" class="penguin4"></div>
        </br>
        <div rel = " fifth" class="penguin5"></div>
        <div rel = " sixth" class="penguin6"></div>
        <div rel = " seventh" class="penguin7"></div>
        <div rel = " eigth" class="penguin8"></div>
    </div>
</body>

1 个答案:

答案 0 :(得分:0)

只需代替this.element即可使用此