2012-02-09 118 views
0
function removeNewsItem(id,idStamp,obj){ 
$('#whiteCover')show(); 

$('#contentOfBox').html= ('Are you sure you want ot completely remove this news item?<br/><br/><div style="text-align:center;"><input type="button" class="button blue medium" onclick="removeNewsItemConfirmed(\'' + id + '\',\''+idStamp+'\','+**obj**+')" value="Yes"/> <input type="button" class="button red medium" value="No" onclick="resetContentBox();"/></div>'); 

$("#whiteCoverContainer").fadeIn(); 
} 

function removeNewsItemConfirmed(id,idStamp,obj){ 
    alert(obj); 
    $(obj).remove(); 
    return; 
} 

<tr><td onclick=removeNewsItem('111','134234',this)></td></tr> 

當我點擊使用就可以了removeNewsItem的TD,我似乎無法能夠通過「這個」元素到下一個功能:用戶點擊後,顯示一條消息,要求確認刪除,但是當他們點擊了「這個」沒有獲得通過properl,或者更重要的是,我不能fihure如何...在javascript中將「this」從一個函數傳遞給另一個函數?

可能有人請幫助:如何傳遞OBJ到另一個內聯onclick事件像上面一樣?

+0

這不是正確使用jQuery方法。你真的知道如何在JavaScript中編程? – 2012-02-09 21:40:09

+0

你忘了引號(「),這並不代表HTML字符串,而是一個dom對象,但」this「應該正確傳遞:http://stackoverflow.com/questions/925734/whats-this-in-javascript -onclick – Julien 2012-02-09 21:41:46

+0

是的,這是什麼代碼:'$('#contentOfBox')。html ='???然後jquery和'onclick' ???我認爲你需要先學習正確的js,嘗試一個更簡單的例子,然後用適當的代碼回到這裏 – elclanrs 2012-02-09 21:43:07

回答

0

this是參考clicked元素。如果您使用自己的onclick處理程序添加新元素,那麼this將參考該處理程序。不是創造它的人。

因此,這將永遠不會工作,你在想

onclick="removeNewsItemConfirmed(\'' + id + '\',\''+idStamp+'\','+**obj**+')" 

相反,你需要創建您可以跟蹤的參考方式。

function removeNewsItem(id,idStamp,obj){ 
    $(obj).addClass('markedForDeletion'); 
    $('#whiteCover')show(); 
    $('#contentOfBox').html= ('Are you sure you want ot completely remove this news item?<br/><br/><div style="text-align:center;"><input type="button" class="button blue medium" onclick="removeNewsItemConfirmed()" value="Yes"/> <input type="button" class="button red medium" value="No" onclick="resetContentBox();"/></div>'); 

    $("#whiteCoverContainer").fadeIn(); 
} 

function removeNewsItemConfirmed(){ 
    $('.markedForDeletion').remove(); 
} 

總的來說,有很多更好的方法來處理所有這些,但希望這會讓你走。

+0

這也是現貨!我對jQuery相當陌生,對我來說這看起來相當整潔...... – John 2012-02-09 22:08:42

1

看起來你應該使用事件。如果你使用jQuery,使用on()來附加事件而不是屬性。 More about on()

使用此功能而不是onclick將返回this您的功能。舉個例子:

<table> 
<tr><td>123</td></tr> 
<tr><td>456</td></tr> 
</table> 

而一些JS

// Will have access to this and has ev which is the event 
function removeNewsItem(ev){ 
    var $this = $(this); 
} 

$('table').on('click', 'td', removeNewsItem); 

如果你需要數據值附加到你的表格單元格,你可以做到這一點與數據屬性和jQuery's data()

+0

這看起來還是最緊密的!我將不得不做一些時間來理解這一切,非常感謝!所有你回覆:) – John 2012-02-09 22:09:00

1

你的事件可能是這樣的(不改變太多現有的標記):

$(".newsItem").click(function(){ 
    var id = $(this).attr('id'); 
    var idStamp = $(this).attr('idStamp'); 
    $('#whiteCover')show(); 

    $('#contentOfBox').html= ('Are you sure you want ot completely remove this news item?<br/><br/><div style="text-align:center;"><input type="button" class="button blue medium" onclick="removeNewsItemConfirmed(\'' + id + '\',\''+idStamp+'\')" value="Yes"/> <input type="button" class="button red medium" value="No" onclick="resetContentBox();"/></div>'); 

    $("#whiteCoverContainer").fadeIn(); 
}); 

function removeNewsItemConfirmed(id,idStamp){    
    alert(id);    
    $("#"+id).remove();    
    return;    
}  

你的HTML,然後將這個樣子:

<tr><td id='111' idStamp='134234' class='newsItem'></td></tr> 
+0

非常感謝!這真棒,我從來沒有想過給​​添加額外的屬性。 – John 2012-02-09 22:06:18

相關問題