我有这段代码,它只是获取一个 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/