Trigger a click event on an inner element

前端 未结 14 520
后悔当初
后悔当初 2020-12-29 07:53

A row in a table where each first cell contains a link needs to be clicked and open a url.

)
    var targetted_elt = ShowEventSource(event);
    //alert('Event\'s target : ' + targetted_elt);

    // Extract the targetted url
    if (targetted_elt == "A") {
        var current_link = GetEventSource(event).href;

    } else {
        var current_tr = GetEventSource(event).parentNode;
        var child_links = current_tr.getElementsByTagName('a');
        var current_link = child_links[0].href;
    }



   // Now open the link
    if(current_link) {
        // Debug  
        alert('will now open href : ' + current_link);
       window.location = current_link;
    }


}

function GetEventSource(event) {
    var e = event || window.event;
    var myelt = e.target || e.srcElement;
    return myelt;
}

function ShowEventSource(event) {
    var elmt;
    var event = event || window.event;            // W3C ou MS
    var la_cible = event.target || event.srcElement;
    if (la_cible.nodeType == 3)            // Vs bug Safari
        elmt = la_cible.parentNode;                        
    else
        elmt = la_cible.tagName;
   return elmt;
}

// Get all document  id's and attach the "click" events to them
  my_rows = new Array();
  my_rows = getElementsByRegExpOnId(/^node_.+/, document , 'tr') ;
    if (my_rows) {
        for (i=0; i< my_rows.length; i++ ) {
            var every_row = document.getElementById( my_rows[i] ) ;
            AddEvent(every_row.id, 'click', FollowSpecialLinks, false);
        }
    }

提交回复
热议问题
14条回答
  •  生来不讨喜
    2020-12-29 08:22

    For the funny purpose of this exercise, here is a pure js solution, i.e., w/o using jQ lib).

    Available here for testing: http://jsfiddle.net/Sr5Vy/3/

    LT5C260A436C41 more data
    LA5C260D436C41 more data

    function AddEvent(id, evt_type, ma_fonction, phase) {
      var oElt = document.getElementById(id);
      if( oElt.addEventListener ) {
          oElt.addEventListener(evt_type, ma_fonction, phase);
      } else if( oElt.attachEvent ) {
          oElt.attachEvent('on'+evt_type, ma_fonction);
      }
    
        // Debug
        // alert('a \'' + evt_type + '\' event has been attached on ' + id );
    
        return false;
    }
    
    function getElementsByRegExpOnId(search_reg, search_element, search_tagName) {
        search_element = (search_element === undefined) ? document : search_element;
        search_tagName= (search_tagName === undefined) ? '*' : search_tagName;
        var id_return = new Array;
        for(var i = 0, i_length = search_element.getElementsByTagName(search_tagName).length; i < i_length; i++) {
            if (search_element.getElementsByTagName(search_tagName).item(i).id &&
            search_element.getElementsByTagName(search_tagName).item(i).id.match(search_reg)) {
                id_return.push(search_element.getElementsByTagName(search_tagName).item(i).id) ;
            }
        }
        return id_return; // array
    }
    
    function FollowSpecialLinks(event) {
    
        // Debug
        // alert('event was successfully attached');
    
        // Prevent propagation
        event.preventDefault();
    
        // Identify targetted node (eg one of the children of