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 badges4 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - event.srcElement.value equivalent in jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743602975a2508875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论