JS rewrite the href attribute and onclick event of a tag

Source: Internet
Author: User

Adapt to the scene: if the mobile phone calls, you need to add an href attribute to the a tag, but because of the need to link jump to add the onclick event to a tag, if you do not do any processing, the default execution of the click event, without jumping the href attribute link.

How to fix: Override the href attribute and onclick event of a tag

This code can be placed in a click event

(function () {

This.fncancel (); var _event_list = {};//Find all the A tags on the page var links = document.getelementsbytagname ("a") [0];//Save the value of the Click Property var _click = Links.getattribute ("onclick");//Save the value of the href attribute var _href = links.getattribute ("href"), if (_click! = null) {//To ONCLI The CK property resets the value Links.setattribute ("onclick", "eval_a_click_event (' A ')");} if (_href! = null) {//re-set the value of the href attribute links.setattribute ("href", "javascript:eval_a_href_event (' A ')");} _event_list["a"] = [links, _href, _click];

})()

function eval_a_href_event (id) {var link = _event_list[id];if (link! = NULL && link[1]! = NULL) {//Gets the value of the href attribute Aler T (Link[1]);//re-assign the href attribute value back to the original value Link[0].setattribute ("href", link[1]);//Remove Click event Link[0].removeattribute ("onclick"); /Impersonation Click event Link[0][0].click ();//Override the value of the href attribute link[0].setattribute ("href", "javascript:eval_a_href_event ('" + ID + "')");// If there is a click event, re-add if (link[2]! = null) {Link[0].setattribute ("onclick", link[2]);}}} function eval_a_click_event (id) {var link = _event_list[id];if (link! = NULL && link[2]! = NULL) {//Get the method of the Click event Aler T (link[2]);//Execute Click event eval (link[2]);}}

JS rewrite the href attribute and onclick event of a tag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.