javascript event.srcElement.value 在 jQuery 中等效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10279459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:17:57  来源:igfitidea点击:

event.srcElement.value equivalent in jQuery

javascriptjquery

提问by Arian

I've got a function that is written in javascriptand I want to rewrite that in jQueryThis is the function code:

我有一个写入的函数,javascript我想重写它,jQuery这是函数代码:

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?

这段代码是什么:event.srcElement.value` 和 jQuery 中的等价物是什么?

Thanks

谢谢

回答by rahul

You can use event.target

您可以使用event.target

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

要找出哪个元素是您可以使用的事件的目标

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

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

回答by Preben Huybrechts

You mean this:

你是这个意思:

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

回答by Niet the Dark Absol

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."

如果你有可用的 JavaScript 代码,不要用 jQuery 重写它。这就像说“这把锤子可以把钉子钉在墙上,但我想用大锤代替。”

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 eventobject, srcElement...)

除此之外,您拥有的代码仅适用于 IE 8 及更低版本,它不适用于 IE 9 或其他浏览器,因为它使用特定术语(全局event对象,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);};

回答by Zymotik

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

这适用于 IE9、IE 8、IE7、Chrome 22、Safari 5.7.1。它在 FF 14 中不起作用:

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