admin管理员组

文章数量:1339781

I've got a function that is written in javascript and I want to rewrite that in jQuery This is the function code:

function onclickRbtn(controlID , Message)
     {
         var s = document.getElementById(controlID);
         var sVal = event.srcElement.value;
         for (var i = 0; i < s.rows.length; i++)
          {
             if (s.getElementsByTagName("input")[i].value == sVal)
                 s.getElementsByTagName("label")[i].innerText = Message;
         }
         s.disabled = 'disabled';
    }

What is this code :event.srcElement.value` and what is equivalent in jQuery?

Thanks

I've got a function that is written in javascript and I want to rewrite that in jQuery This is the function code:

function onclickRbtn(controlID , Message)
     {
         var s = document.getElementById(controlID);
         var sVal = event.srcElement.value;
         for (var i = 0; i < s.rows.length; i++)
          {
             if (s.getElementsByTagName("input")[i].value == sVal)
                 s.getElementsByTagName("label")[i].innerText = Message;
         }
         s.disabled = 'disabled';
    }

What is this code :event.srcElement.value` and what is equivalent in jQuery?

Thanks

Share Improve this question edited Apr 24, 2012 at 7:20 Anuj Balan 7,74123 gold badges61 silver badges94 bronze badges asked Apr 23, 2012 at 11:13 DooDooDooDoo 13.5k70 gold badges191 silver badges318 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

You can use event.target

To find out which element is the target of the event you can use

*http://www.quirksmode/js/events_properties.html#target*

You mean this:

$("button").click(function(){
    var value = $(this).val();
});

If you have working JavaScript code, DO NOT rewrite it in jQuery. That's like saying "this hammer can push nails into a wall, but I want to use a sledgehammer instead."

That aside, the code you have is exclusive to IE 8 and below, it won't work in IE 9 or other browsers, because it uses specific terms (global event object, srcElement...)

Here's the code you should use:

function onclickRbtn(controlID,Message,evt) {
    evt = evt || window.event;
    var s = document.getElementById(controlID),
        sVal = (evt.srcElement || evt.target).value,
        inputs = s.getElementsByTagName('input'),
        labels = s.getElementsByTagName('label');

    for( var i=0; i<s.rows.length; i++) {
        if( inputs[i].value == sVal) labels[i].innerText = labels[i].textContent = Message;
    }
    s.disabled = true;
}

And wherever you attach the event, make sure you add the event to the arguments. Examples:

<element onclick="onclickRbtn(controlID, Message, event);">
elem.onclick = function(e) {onclickRbtn(controlID, Message, e);};

This works in IE9, IE 8, IE7, Chrome 22, Safari 5.7.1. It does not work in FF 14:

$(event.srcElement || event.target);

本文标签: javascripteventsrcElementvalue equivalent in jQueryStack Overflow