【问题标题】:Javascript calling functions on inputsJavascript 在输入上调用函数
【发布时间】:2014-08-19 17:41:33
【问题描述】:

当我单击我的输入来更改字段的背景颜色时,我想要。我想通过调用一个函数来做到这一点。我究竟做错了什么 ?谢谢!

<input type="text" onFocus="makefieldred();" onBlur="makefieldwhite();" >


<script>
function makefieldred() {
    this.style.backgroundcolor='red' ;
    }
function makefieldwhite() {
    this.style.backgroundcolor='white' ;
    }
</script>

【问题讨论】:

标签: javascript function field


【解决方案1】:

this 在你的函数调用中不是元素,因为你连接它们的方式。另外,属性名称是backgroundColor(大写C)而不是backgroundcolor

如果要使用onXyz属性机制,最简单的方法就是元素传入函数中:

<input type="text" onFocus="makefieldred(this);" onBlur="makefieldwhite(this);" >


<script>
function makefieldred(elm) {
    elm.style.backgroundColor='red' ;
}
function makefieldwhite(elm) {
    elm.style.backgroundColor='white' ;
}
</script>

或者,使用现代技术来设置事件处理程序:

<input type="text" id="the Input">


<script>
(function() {
    var input = document.getElementById("theInput");
    input.addEventListener("focus", makefieldred, false);
    input.addEventListener("blur", makefieldwhite, false);

    function makefieldred() {
        this.style.backgroundColor='red' ;
    }
    function makefieldwhite() {
        this.style.backgroundColor='white' ;
    }
</script>

如果您必须支持 IE8,如果在 input 上看不到 addEventListener,则需要使用 attachEvent。要处理浏览器不兼容问题,您可以使用函数 (taken from my other answer here):

var hookEvent = (function() {
    var div;

    // The function we use on standard-compliant browsers
    function standardHookEvent(element, eventName, handler) {
        element.addEventListener(eventName, handler, false);
        return element;
    }

    // The function we use on browsers with the previous Microsoft-specific mechanism
    function oldIEHookEvent(element, eventName, handler) {
        element.attachEvent("on" + eventName, function(e) {
            e = e || window.event;
            e.preventDefault = oldIEPreventDefault;
            e.stopPropagation = oldIEStopPropagation;
            handler.call(element, e);
        });
        return element;
    }

    // Polyfill for preventDefault on old IE
    function oldIEPreventDefault() {
        this.returnValue = false;
    }

    // Polyfill for stopPropagation on old IE
    function oldIEStopPropagation() {
        this.cancelBubble = true;
    }

    // Return the appropriate function; we don't rely on document.body
    // here just in case someone wants to use this within the head
    div = document.createElement('div');
    if (div.addEventListener) {
        div = undefined;
        return standardHookEvent;
    }
    if (div.attachEvent) {
        div = undefined;
        return oldIEHookEvent;
    }
    throw "Neither modern event mechanism (addEventListener nor attachEvent) is supported by this browser.";
})();

然后:

<script>
(function() {
    var input = document.getElementById("theInput");
    hookEvent(input, "focus", makefieldred);
    hookEvent(input, "blur", makefieldwhite);

    function makefieldred() {
        this.style.backgroundColor='red' ;
    }
    function makefieldwhite() {
        this.style.backgroundColor='white' ;
    }
</script>

【讨论】:

  • 当我看到这些问题时,我需要随身携带笔记本电脑,而不是试图通过 iPad 打败任何人......
  • 我认为它需要是elm.style.backgroundColor,大写“C”。
  • @epascarello:非常 好点,我有点看一般而不是具体。 (实际上并没有那么多代码,但如果 [改变颜色} 是唯一的最终目标,那么 CSS 将是实现它的方法。)
  • 非常感谢!!!我刚开始学习javascript!是的,最简单的方法是在 CSS 中进行,但我只想了解 javascript 的工作原理! :)
  • @InFamouStarlight:不客气,很高兴有帮助。也请查看 CSS 解决方案(epascarello 在评论中链接信息),但以上内容将帮助您解决 CSS 无法完成工作的情况。
猜你喜欢
  • 2010-11-03
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 2019-08-17
  • 2021-06-03
  • 1970-01-01
相关资源
最近更新 更多