2013-10-22 31 views
0

我有一個JavaScript函數,運行一些代碼,點擊激活按鈕時 此功能切換(改變) 到 切換,但我想這個代碼更具體 我要發送的ID給這個函數和切換隻對象如何獲得特定按鈕的ID和javascript函數

例如我這樣做: 但我不能得到該鏈接的ID在功能

還可以看到我的代碼如下:

這是我的javascript函數:

<script type="text/javascript"> 

    $('document').ready(function(){ 
     $('a.activation').click(function(){ 
      var de_id = $(this).attr('de_id'); 
      var parent = $(this).parent(); 
      $.post('insert.php', {de_id:de_id}); 
      $('a.activation').toggle(); 
     }); 

     $('a.activation').click(function(){ 
      $('a.activation').toggle(); 
      var act_id = $(this).attr('act_id'); 
      var parent = $(this).parent(); 
      $.post('insert.php', {act_id:act_id}); 
      $('a.activation').toggle(); 
     }); 
    }); 

</script> 

,這是身體:

<td><?php if($news['ns_act']==1){ 
         echo "<a class='activation' onClick=\"reply_click(this.{$news['ns_id']})\" href=\"javascript:return(0);\" de_id=\"{$news['ns_id']}\" style=\"display: inline-block;\" ><img src=\"images\icons\activate.png\" height=\"16px\" width=\"16px\" /></a> 
           <a class='activation' onClick=\"reply_click(this.{$news['ns_id']})\" href=\"javascript:return(1);\" act_id=\"{$news['ns_id']}\" style=\"display:none;\" ><img src=\"images\icons\deactivate.png\" height=\"16px\" width=\"16px\" /></a>"; 
       } 
       else { 
        echo "<a class='activation' onClick=\"reply_click(this.{$news['ns_id']})\" href=\"javascript:return(1);\" act_id=\"{$news['ns_id']}\" style=\"display: inline-block;\" ><img src=\"images\icons\deactivate.png\" height=\"16px\" width=\"16px\" /></a> 
          <a class='activation' onClick=\"reply_click(this.{$news['ns_id']})\" href=\"javascript:return(0);\" de_id=\"{$news['ns_id']}\" style=\"display:none;\"><img src=\"images\icons\activate.png\" height=\"16px\" width=\"16px\" /></a>"; 
       } 

       ?></td> 

回答

0

JS

$('document').ready(function(){ 
     $('a.activation').click(function(e){ 
      var de_id = $(this).attr('de_id'); 
      var parent = $(this).parent(); 
      $.post('insert.php', {de_id:de_id}); 
      $("#"+e.target.id).toggle(); 
     }); 

     $('a.activation').click(function(e){ 
      $('a.activation').toggle(); 
      var act_id = $(this).attr('act_id'); 
      var parent = $(this).parent(); 
      $.post('insert.php', {act_id:act_id}); 
      $("#"+e.target.id).toggle(); 
     }); 
    }); 

</script> 

HTML

echo "<a id='someid' class='activation' onClick=\"reply_click(this.{$news['ns_id']})\" href=\"javascript:return(0);\" de_id=\"{$news['ns_id']}\" style=\"display: inline-block;\" ><img src=\"images\icons\activate.png\" height=\"16px\" width=\"16px\" /></a> 
<a id='someotherid' class='activation' onClick=\"reply_click(this.{$news['ns_id']})\" href=\"javascript:return(1);\" act_id=\"{$news['ns_id']}\" style=\"display:none;\" ><img src=\"images\icons\deactivate.png\" height=\"16px\" width=\"16px\" /></a>"; 
+0

你可以給我一些提示嗎? e.target.id? 我應該在我的代碼中做什麼? –

+0

它沒有工作! –

+0

你需要添加ID字段到你的HTML中的鏈接,你回聲 –

0
<script type="text/javascript"> 

     $('document').ready(function(){ 
      switch=0; 
      $('a.activation').click(function(){ 
       if(switch==0){ 
        var de_id = $(this).attr('de_id'); 
        var parent = $(this).parent(); 
        $.post('insert.php', {de_id:de_id}); 
        switch=1; 
        $(this).css("background","green").html('Activate'); 
        } 
       else{ 
        var act_id = $(this).attr('act_id'); 
        var parent = $(this).parent(); 
        $.post('insert.php', {act_id:act_id}); 
        switch=0; 
        $(this).css("background","red").html('Deactivate'); 
        } 
      }); 

    });// ready end 

</script> 

HTML

<a class='activation' onClick=\"reply_click(this.{$news['ns_id']})\" href=\"javascript:return(0);\"act_id=\"{$news['ns_id']}\" de_id=\"{$news['ns_id']}\" style=\"display: inline-block;\" ><img src=\"images\icons\activate.png\" height=\"16px\" width=\"16px\" /></a> 
+0

'$(this).css(「background」,「red」)。html('Deactivate');'代替這個,你可以更改'圖片'源 –

+0

謝謝你是我的結構正確,但真正的問題是關於點擊按鈕的ID! –

+0

你能解釋一下你在哪裏使用ID? –

相關問題