Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查onclick事件触发的表单元素_Javascript_Jquery_Forms_Dom - Fatal编程技术网

Javascript 检查onclick事件触发的表单元素

Javascript 检查onclick事件触发的表单元素,javascript,jquery,forms,dom,Javascript,Jquery,Forms,Dom,我在一个网页中有一个js函数,可以捕捉点击的位置。我必须以一种能够识别页面是否包含表单元素的方式来实现它,如果是,则检查我正在录制的点击是否是在一个随机表单元素上进行的。 我站起来检查表单并学习其元素,但我不知道如何检查是否进行了单击​​在单个元素上: $('*').on('mousedown', function (e) { // make sure the event isn't bubbling if (e.target != this) { return; } // do

我在一个网页中有一个js函数,可以捕捉点击的位置。我必须以一种能够识别页面是否包含表单元素的方式来实现它,如果是,则检查我正在录制的点击是否是在一个随机表单元素上进行的。 我站起来检查表单并学习其元素,但我不知道如何检查是否进行了单击​​在单个元素上:

$('*').on('mousedown', function (e) {
 // make sure the event isn't bubbling
 if (e.target != this) {
    return;
 }
 // do something with the click somewhere
 // for example, get the coordinates:
 x = e.pageX;
 y = e.pageY;
 pa = $(location).attr('href');
 //Window dimension
 var width = $(window).width();
 //var width = $getWidth();
 var height = $(window).height();

 //Check if page contain a form
 var elementF =  document.querySelectorAll('form');
 var fname = '';
 var elements = '';
 for (var i=0; i<elementF.length; i++){
  fname = elementF[i].id;
  elements = document.forms[fname].elements;
  for (x=0; x<elements.length; x++){
    //Now i need to check if element is clicked or not
  }
}
$('*')。在('mousedown',函数(e)上{
//确保活动没有冒泡
如果(例如,目标!=此){
回来
}
//用鼠标点击某处做点什么
//例如,获取坐标:
x=e.pageX;
y=e.pageY;
pa=$(位置).attr('href');
//窗口尺寸
变量宽度=$(窗口).width();
//var width=$getWidth();
var height=$(window.height();
//检查页面是否包含表单
var elementF=document.querySelectorAll('form');
var fname='';
var元素=“”;
对于(var i=0;iTry


这里要做的是将mousedown事件绑定到页面上的每个元素。这将是资源密集型的,不应该是必需的。此外,由于它到处都绑定,因此
if(e.target!==This){return;}
确实可以防止它在你用鼠标按下某个东西时触发一百万次,但也可以防止它冒泡。让它冒泡,然后用
e.target
e.currentTarget
来区分东西。不应该有任何理由这样做
$('*')。on(“mousedown”)

你为什么不能这么做

$("form").on("click", function(e){
    // e.currentTarget will be the form element capturing the click

    console.log( $(e.currentTarget).html() );

    // e.target will be whatever element you clicked on
    // so you can do whatever you want with it

    console.log( $(e.target).text() );

    // All your handler code here
});
一些可能有用的东西


哇……你可能只是说你想使用一个id?你应该给你的表单一个id属性,然后使用
document.getElementById(这里是id_)
。我已经知道表单是否存在,并且我已经知道它的所有元素的id,我需要知道我正在录制的单击是否在其中一个项目上完成
$("form").on("click", function(e){
    // e.currentTarget will be the form element capturing the click

    console.log( $(e.currentTarget).html() );

    // e.target will be whatever element you clicked on
    // so you can do whatever you want with it

    console.log( $(e.target).text() );

    // All your handler code here
});