2012-02-09 69 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

相关问题