如何在jQuery中添加和删除事件?
我有一个图像,在.cs文件中为“a href”标记定义了一个InnerHTML,如下所示如何在jQuery中添加和删除事件?,jquery,Jquery,我有一个图像,在.cs文件中为“a href”标记定义了一个InnerHTML,如下所示 HtmlGenericControl _divToolTipContainer = new HtmlGenericControl("div"); _divToolTipContainer.InnerHtml = "<a href=\"javascript:__doPostBack('" + btnItemThumbnail.ClientID.Replace("_", "$") + "','')\"&g
HtmlGenericControl _divToolTipContainer = new HtmlGenericControl("div");
_divToolTipContainer.InnerHtml = "<a href=\"javascript:__doPostBack('" + btnItemThumbnail.ClientID.Replace("_", "$") + "','')\">" +
<img src='" + string.Format("ItemPreview.axd/{0}/width-250/height-328/", item.ID.ToString()) + "'></a>;
var objectDiv = document.getElementById(oElementId); // get the object id
If (false){ // condition here
remove the event of the a href tag..
jquery code must be added here..
}else{
add the event again of the a href tag..
jQuery code must be added here..
}
如何做到这一点
谢谢
Jason您也可以使用jQuery根据ID选择元素,因此
(function($) {
var objectDiv = $("#"+oElementId); // get the object id
if(false)
{ // condition here
objectDiv.removeAttr("href");
}
else
{
objectDiv.attr("href", "javascript:__doPostBack('" + btnItemThumbnail.ClientID.Replace("_", "$") + "','')");
}
})(jQuery);
编辑:您可能必须将代码包装在(函数($)中,以确保代码使用$forjQuery现在尝试我的上面的代码。(注意:您必须将要在其中的
中使用的所有内容包装起来。)现在仍在工作,我仍然可以单击图像,它将转到提供的a href位置…如果它只是转到“在此处添加您的事件”或我以前放在那里的任何内容,那么代码一定在工作,但我没有将您的javascript代码放在那里,但我现在已使用该代码编辑了我的答案。
if (false) {
$('#oElementId').off('click', function() {});
} else {
$('#oElementId').on('click', function() {});
}