javascript - GetElementById 设置为 null 而不是 int

标签 javascript php jquery html

我有这段代码,它只是获取一个 id 并将其分配给一个变量,由于某种原因,notification_id 设置为 null,而它应该是来自 php for 循环的整数

JavaScript

$(".pro_content_notification_remove").live('click', function (e) {

 var notification_id = document.getElementById(this);

 e.preventDefault(); 
 $(this).parent().remove();

});

php/html

<?php 
    foreach($notification_array as $key => $value){



       echo '<div class="pro_content_notification_ele" id="'.$value.'">
                                    <div class="pro_content_notification_image '.$notification_image_id.'"></div><!--end pro_content_notifications_image-->
                                    <span class="pro_content_notification_text '.$viewed.'">'.$notification_text.'</span>
                                    <div class="pro_content_notification_remove"></div><!--end pro_content_notification_remove-->
                                </div><!--end pro_content_notification_ele-->';
                    }

    ?>

我检查了数组和页面源,pro_content_notification_ele的id中设置的$value肯定是1,为什么设置为null。

感谢您的帮助

最佳答案

this 引用事件处理程序内的 dom 元素,因此使用 this.id 获取元素的 ID。

$(".pro_content_notification_remove").live('click', function (e) {
    var notification_id = this.id;

    e.preventDefault(); 
    $(this).parent().remove();
});

注意:如果您使用的是 jQuery >= 1.7,请使用 .on()而不是 .live()

关于javascript - GetElementById 设置为 null 而不是 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21654893/

相关文章:

javascript - 正在使用 var foo = function foo() {};在 IE < 9 中创建内存泄漏的表达式?

javascript - iframe onload 在 iframe 未准备好时触发

javascript - Uncaught ReferenceError : onchange function is not defined (wordpress)

php - 准备好的语句 : error near '? WHERE ` quantity`=? '

javascript - 将自定义按钮添加到传单 map

php - 在 Laravel 中创建一个进程来动态监听、工作或监控队列

php - nginx 真实客户端 ip 不起作用

javascript - Bootstrap Modals 在关闭后继续向 body 添加 padding-right

javascript - 将选择框事件中添加的动态表单元素依次对齐到选择框上方的页面顶部

javascript - XHR2 进度回退