简体   繁体   English

更改超级链接的onclick

[英]Change the onclick of a hyper link

I want to update the click event of the hyperlink onClick and want to update the html inside the hyperlink in jquery 我想更新onClick上超链接的click事件,并希望更新jquery中超链接内的html

HTML HTML

<a class=" save" href="javascript:void(0);" onclick="save_unsave('unsave','UID','ID',this);">
    <div class="jopt_iconwrap" title="Unsave" alt="Unsave" >
        <span class="saved"></span>
    </div>
    Unsave
</a> 

Now onlick of save_unsave function i want to update the html design as below: 现在点击save_unsave函数我想更新html设计如下:

 <a class=" save" href="javascript:void(0);" onclick="save_unsave('save','UID','ID',this);">
        <div class="jopt_iconwrap" title="Save" alt="Save" >
            <span class=""></span>
        </div>
        Save
    </a> 

I am trying to do this in the function: 我试图在函数中执行此操作:

jQuery jQuery的

if(type == 'save')
    {
            var new_type = "'unsave'";
            jQuery(elem_obj).html("");
            jQuery(elem_obj).html('<a onclick="save_unsave('+new_type+','+uid+','+id+',this);" class="saved" href="javascript:void(0);"><div class="jopt_iconwrap" title="Unsave" alt="Unsave"><span class="saved"></span></div>Unsave</a>');
    }
    else if(type == 'unsave')
    {

        var new_type = "'save'";
        jQuery(elem_obj).html("");
        jQuery(elem_obj).html('<a onclick="save_unsave('+new_type+','+uid+','+id+',this);" class=" save" href="javascript:void(0);"><div class="jopt_iconwrap" title="Unsave" alt="Unsave"><span class="mico"></span></div>Save</a>');

    }

How can we do this in jquery.Please help me in the i have the elmenet object in the function. 我们怎么能在jquery中这样做。请帮助我在函数中有elmenet对象。

我建议您使用replaceWith()代替.html()它将适合您。

 $('a').click(function() { if ($(this).hasClass('save')) { $(this).removeClass('save').addClass('unsave'); $(this).text('Unsave') $(this).attr('id','save') } else { $(this).removeClass('unsave').addClass('save'); $(this).text('Save') $(this).attr('id','unsave') } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a class=" save"> <div class="jopt_iconwrap" title="Save" alt="Save"> <span class=""></span> </div> Save </a> 

Try this way 试试这种方式

You can replace the onclick event, like this: 您可以替换onclick事件,如下所示:

$('.save')[0].onclick = function() {
  save_unsave('save', 'UID', 'ID', this);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM