简体   繁体   English

JavaScript无法在Internet Explorer中运行

[英]JavaScript Not Working in Internet Explorer

I'm currently styling my username/password inputs on a webpage. 我目前正在网页上设置用户名/密码输入的样式。 They are working as intended in Firefox/Safari, but Internet Explorer throws the following error: 它们在Firefox / Safari中按预期工作,但是Internet Explorer引发以下错误:

Line: 7, Char: 2, Objected required 行:7,字符:2,需要异议

It happens when I give focus to my inputs. 当我专注于输入时,就会发生这种情况。 This function is called onfocus: 此功能称为onfocus:

function InputFocused(InputID)
{
    var InputObject = document.getElementById(InputID);
    DefaultValue = InputObject.value;
    InputObject.value = "";
    InputObject.style.color = "#000";
}

And this is the markup: 这是标记:

<input type="text" class="login" id="username" value="USERNAME" onfocus="InputFocused(this.id)" onblur="InputBlurred(this.id)" />

I thought this was pretty straightforward, but I have extremely little experience with JavaScript, so any insight would be appreciated. 我以为这很简单,但是我对JavaScript的经验很少,所以任何见识都会受到赞赏。

I think that your best bet would be to do this 我认为您最好的选择就是这样做

   function InputFocused(DOM_OBJECT)
    {
        DefaultValue = DOM_OBJECT.value;
        DOM_OBJECT.value = "";
        DOM_OBJECT.style.color = "#000";
    }

While your HTML would look like this: HTML如下所示:

<input type="text" class="login" "id="username" value="USERNAME" onfocus="InputFocused(this)" onblur="InputBlurred(this)" />

This will pass the complete DOM object, rather than just passing the id. 这将传递完整的DOM对象,而不只是传递id。

I hope this helps! 我希望这有帮助!

You might want to add a sanity check by alert ing the value of InputID . 您可能需要通过alert InputID的值来添加完整性检查。 There's a problem with your html ( "id= instead of id= ) but that might just be the result of a quick copy/paste. 您的html有一个问题( "id=而不是id= ),但这可能只是快速复制/粘贴的结果。

Also: I notice that you're passing in the value of the id attribute and then looking up the element instead of just passing a reference to the element directly. 另外:我注意到您正在传递id属性的值,然后查找该元素,而不是直接传递对该元素的引用。

I think this 我认为这

<input type="text" class="login" "id="username" value="USERNAME" onfocus="InputFocused(this.id)" onblur="InputBlurred(this.id)" />

would need to be this 需要是这个

<input type="text" class="login" id="username" value="USERNAME" onfocus="InputFocused('username')" onblur="InputBlurred('username')" />

for the example given. 对于给出的例子。

What version of IE are you using? 您正在使用哪个版本的IE?

The problem, I expect, is that when you're specifying onfocus="InputFocused(this.id)" I'm not sure IE knows what it's current context is and as a guess I'd say its because the DOM has not finished being built. 我期望的问题是,当您指定onfocus =“ InputFocused(this.id)”时,我不确定IE知道它的当前上下文是什么,并且我猜想是因为DOM尚未完成正在建设。

By way of a solution and explanation you can acheieve what you are trying to do like this: 通过解决方案和解释,您可以实现以下目的:

HTML: HTML:

<input type="text" class="login" id="username" value="USERNAME"/>

Then in your javascript you can specify and set your event handlers: 然后,您可以在javascript中指定并设置事件处理程序:

JAVASCRIPT: JAVASCRIPT:

       //get the element...
    var txtUsrNm =  document.getElementById('username');

    //specify the event handler for focus
    txtUsrNm.onfocus = function(){
      this.value = "";
      this.style.color = "#000";
    }

//specify the event handler for blur
    txtUsrNm.onblur = function(){
      this.style.color = "#f00";
    }

I've left out your DefaultValue bit as I wasn't fully aware of what you were trying to do with it. 由于我不完全了解您要使用的默认值,因此省略了您的DefaultValue位。 I haven't tested the code so sorry if it's not working. 我还没有测试过代码,如果无法正常工作,我们感到抱歉。 you can improve it all by wrapping in a function etc but thats probably a bit off topic. 您可以通过包装函数等来改善所有功能,但这可能与主题无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM