什么是使函数仅在活动/聚焦输入/按钮/文本区域上工作的jQuery或javaScript语法? [英] What is the jQuery or javaScript syntax to make functions work only on the active/focused input/button/text area?

查看:59
本文介绍了什么是使函数仅在活动/聚焦输入/按钮/文本区域上工作的jQuery或javaScript语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者&自我感兴趣的网络编码器。

I am a beginner & self interested web coder.

我一直在测试,询问,重新测试,尝试,阅读javaScript中的不同功能解决方案到在线表单,其中包含大量的一旦我完成,< textarea>的

I have been testing, asking, retesting, trying, reading up on different functionality solutions in javaScript to an online form which will consist of a multitude of <textarea>'s once I am done.

我相当不错,目前的功能状态,这是基于几个js事件。示例代码将是(写得很有趣,因此在论坛中更容易阅读,实际代码显然是一行):

I am fairly ok, with the current state of functions, which are based upon several js events. Example code would be (written funny so it is easier to read in the forum, actual code is one line obviously):

<textarea
    data-id="0"
    class="classOne classTwo"
    id="dataInput_0"
    name="xInput_row_1"
        onFocus="functionOne();"
        onBlur="functionTwo();"
        onKeyUp="functionThree();">
</textarea>

我构建并测试了所有函数,专门用于 id = dataInput_0使用 getElementById 。示例:

I built and tested all the functions to work specifically on the id="dataInput_0" using getElementById. Example:

var d = document.getElementById("dataInput_0");

所以我的问题是如何让函数触发其他dataInput id's?

So my question is how to I make the functions trigger for other "dataInput" id's?

换句话说:

var d = document.getElementById('whichever dataInput that is active/focused');

谢谢!

推荐答案

使用当前代码的最简单方法是:

The simplest way to work with your current code would be to do this:

onFocus="functionOne(this);"

...然后定义你的功能:

...and then define your function:

function functionOne(el) {
   // el is the element in question, used e.g.:
   alert(el.name);
}

onFocus = ... 浏览器将设置为相关元素,因此您可以将其作为参数传递给您的函数。你的函数直接使用它而不是通过 getElementById()

Within the onFocus=... the browser sets this to the element in question, so you can then pass it as a parameter to your function. Your function then just uses it directly rather than having to go via getElementById().

但是你提到了jQuery ,你可以从你的html中删除内联 onFocus 和其他 onXYZ 处理程序,只需在你的JS中执行以下操作,如下所示:

But since you mentioned jQuery, you could remove the inline onFocus and other onXYZ handlers from your html and just do it all in your JS as follows:

$("textarea").focus(function() {
   // here this is the element in question, e.g.:
   alert(this.value);
});

为页面上的所有textareas定义焦点处理程序 - 将其缩小到只有textareas类classOne执行 $(textarea.classOne)。在函数中,此指的是焦点元素。您可以使用 .blur() keyup()方法为代码中显示的其他事件分配处理程序。

That defines a focus handler for all textareas on the page - to narrow it down to just textareas with class "classOne" do $("textarea.classOne"). Within the function this refers to the focused element. You could use the .blur() and keyup() methods to assign handlers for the other events shown in your code.

这篇关于什么是使函数仅在活动/聚焦输入/按钮/文本区域上工作的jQuery或javaScript语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆