2014-02-09 64 views
0

我有這段代碼,僅僅得到一個編號,並將其分配給出於某種原因notification_id一個變量設置爲空時,它應該是從PHP for循環的getElementById設置爲null,而不是INT

的整數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被設置爲1肯定,那麼,爲什麼是空被設置。

感謝您的幫助

+0

的Insetead'this'不是一個字符串,而'getElementById'應該返回一個'HTMLNode '。 –

+2

你以一種奇怪的方式混合jquery和'regular'js – 2014-02-09 03:38:16

回答

1

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()

1

嘗試使用,

var notification_id = e.target.id; 

var notification_id = $(this).attr('id'); 

var notification_id = this.id; 

代替,

var notification_id = document.getElementById(this); 
1

使用

document.getElementById(this) 

使用

$(this).attr('id') 
+1

爲什麼不只是'this.id'?這裏使用jQuery有什麼意義? –