使用 Javascript 更改元素的 onfocus 处理程序?

发布于 2024-07-08 14:35:04 字数 793 浏览 11 评论 0原文

我有一个表单,其中的默认值描述了应该输入该字段的内容(替换标签)。 当用户聚焦某个字段时,将调用此函数:

function clear_input(element)
{
    element.value = "";
    element.onfocus = null;
}

onfocus 设置为 null,这样,如果用户在该字段中放置了某些内容并决定更改它,他们的输入不会被删除(因此只会被删除一次)。 现在,如果用户在不输入任何数据的情况下移动到下一个字段,则使用此函数(称为 onblur)恢复默认值:

function restore_default(element)
{
    if(element.value == '')
    {
        element.value = element.name.substring(0, 1).toUpperCase()
                          + element.name.substring(1, element.name.length);
    }
}

恰好默认值是元素的名称,因此无需添加 ID ,我只是操纵了 name 属性。 问题是,如果他们确实跳过了该元素,那么 onfocus 事件将被clear_input 取消,但随后永远不会恢复。

我添加了

element.onfocus = "javascript:clear_input(this);";

Restore_default 函数,但这不起作用。 我该怎么做呢?

I have a form that has default values describing what should go into the field (replacing a label). When the user focuses a field this function is called:

function clear_input(element)
{
    element.value = "";
    element.onfocus = null;
}

The onfocus is set to null so that if the user puts something in the field and decides to change it, their input is not erased (so it is only erased once). Now, if the user moves on to the next field without entering any data, then the default value is restored with this function (called onblur):

function restore_default(element)
{
    if(element.value == '')
    {
        element.value = element.name.substring(0, 1).toUpperCase()
                          + element.name.substring(1, element.name.length);
    }
}

It just so happened that the default values were the names of the elements so instead of adding an ID, I just manipulated the name property. The problem is that if they do skip over the element then the onfocus event is nullified with clear_input but then never restored.

I added

element.onfocus = "javascript:clear_input(this);";

In restore_default function but that doesn't work. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

游魂 2024-07-15 14:35:04

使用

element.onfocus = clear_input;

or (带参数)

element.onfocus = function () { 
    clear_input( param, param2 ); 
};

function clear_input () {
    this.value = "";
    this.onfocus = null;
}

“javascript:”位是不必要的。

Use

element.onfocus = clear_input;

or (with parameters)

element.onfocus = function () { 
    clear_input( param, param2 ); 
};

with

function clear_input () {
    this.value = "";
    this.onfocus = null;
}

The "javascript:" bit is unnecessary.

森林散布 2024-07-15 14:35:04

看起来您不允许字段为空,但是如果用户在字段中放置一个或多个空格怎么办? 如果你想防止这种情况发生,你需要修剪它。 (请参阅 Steven Levithan 博客了解不同的修剪方法)。

function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

如果您确实想将字符串大写,您可以使用:

function capitalize(str) {
    return str.substr(0, 1).toUpperCase() + str.substr(1).toLowerCase();
}

通过清除 onfocus 事件,您已经创建了一个不应该出现的问题。 一个更简单的解决方案是在 onfocus 事件中添加一个 if 语句,这样它只会清除它是否是您的默认值(但我更喜欢像 tvanfosson 建议的那样选择它)。

我假设您在输入元素上设置了 value-property,以便在显示页面时,即使禁用 javascript,输入元素中也会显示一个值。 该值可用作 element.defaultValue。 使用这种方法的好处:

  • 您只需在一处定义默认值。
  • 您不再需要将处理程序中的任何值资本化。
  • 默认值可以有任何大小写(例如“John Y McMain”)。
  • 默认值不再需要与元素的名称相同。

function clear_default(element) {
    if (trim(element.value) == element.defaultValue ) { element.value = ""; }
}

function restore_default(element) {
    if (!trim(element.value).length) { element.value = element.defaultValue;}
}

It looks like you don't allow the fields to be empty, but what if the user puts a single or more spaces in the field? If you want to prevent this, you need to trim it. (See Steven Levithans blog for different ways to trim).

function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

If you really want to capitalize the strings you could use:

function capitalize(str) {
    return str.substr(0, 1).toUpperCase() + str.substr(1).toLowerCase();
}

By clearing the onfocus event you have created a problem that should not have been there. An easier solution is to just add an if-statement to the onfocus event, so it only clears if it is your default value (but I prefer to select it like tvanfosson suggested).

I assume that you on your input-elements have set the value-property so that a value is shown in the input-elements when the page is displayed even if javascript is disabled. That value is available as element.defaultValue. Bonuses by using this approach:

  • You only define the default value in one place.
  • You no longer need to capitalize any value in your handlers.
  • The default value can have any case (like "John Y McMain")
  • The default value no longer needs to be the same as the name of the element.

.

function clear_default(element) {
    if (trim(element.value) == element.defaultValue ) { element.value = ""; }
}

function restore_default(element) {
    if (!trim(element.value).length) { element.value = element.defaultValue;}
}
撑一把青伞 2024-07-15 14:35:04

我建议你以不同的方式处理它。 与其清除该值,为什么不将其全部突出显示,以便用户可以开始键入以覆盖它。 然后您不需要恢复默认值(尽管如果该值为空,您仍然可以这样做并且以相同的方式)。 您还可以将处理程序保留在适当的位置,因为文本不会被清除,只是突出显示。 使用验证来确保该值不是输入的原始值。

function hightlight_input(element) {
    element.select();
}

function restore_default(element) // optional, do we restore if the user deletes?
{
    if(element.value == '')
    {
        element.value = element.name.substring(0, 1).toUpperCase()
                          + element.name.substring(1, element.name.length);
    }
}

I would suggest that you handle it a little differently. Instead of clearing the value, why not just highlight it all so that the user can just start typing to overwrite it. Then you don't need to restore the default value (although you could still do so and in the same way if the value is empty). You also can leave the handler in place since the text is not cleared, just highlighted. Use validation to make sure the value is not the original value of the input.

function hightlight_input(element) {
    element.select();
}

function restore_default(element) // optional, do we restore if the user deletes?
{
    if(element.value == '')
    {
        element.value = element.name.substring(0, 1).toUpperCase()
                          + element.name.substring(1, element.name.length);
    }
}
り繁华旳梦境 2024-07-15 14:35:04
<!-- JavaScript
function checkClear(A,B){if(arguments[2]){A=arguments[1];B=arguments[2]} if(A.value==B){A.value=""} else if(A.value==""){A.value="Search"}}
//-->

<form method="post" action="search.php">
<input type="submit" name="1">
<input type="text" name="srh" Value="Search" onfocus="checkClear(this,'Search')" onblur="checkClear(this,' ')">
</form>
<!-- JavaScript
function checkClear(A,B){if(arguments[2]){A=arguments[1];B=arguments[2]} if(A.value==B){A.value=""} else if(A.value==""){A.value="Search"}}
//-->

<form method="post" action="search.php">
<input type="submit" name="1">
<input type="text" name="srh" Value="Search" onfocus="checkClear(this,'Search')" onblur="checkClear(this,' ')">
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文