Javascript Can';无法在JQuery中获取输入焦点父级?
当我专注于表单组内的输入时,为什么无法获取表单组的父级?在输入焦点上,我在Firefox中得到一个在控制台中返回的对象:{length:1,prevObject:object,context:,1 more…}但是,这不是我感兴趣的对象。this和.parent()都返回相同的HTML节点。我正在使用Javascript Can';无法在JQuery中获取输入焦点父级?,javascript,jquery,Javascript,Jquery,当我专注于表单组内的输入时,为什么无法获取表单组的父级?在输入焦点上,我在Firefox中得到一个在控制台中返回的对象:{length:1,prevObject:object,context:,1 more…}但是,这不是我感兴趣的对象。this和.parent()都返回相同的HTML节点。我正在使用console.log($(this.parent())inputFocus方法内部的代码 HTML: Javascript: var formAutoGenerator = function(
console.log($(this.parent())代码>inputFocus方法内部的代码
HTML:
Javascript:
var formAutoGenerator = function() {
var self = this; //$ has it's own 'this'
this.repeatGroup = $('.repeat-group');
this.formGroupLength = this.repeatGroup.length;
this.init = function(){
this.inputFocus();
this.correctClassAdded();
};
this.classTag = 'repeat-group';
this.correctClassAdded = function(formGroup){
// console.log( $(formGroup).offsetParent() );
return formGroup;
};
this.inputFocus = function() {
$(document).ready(function(){
$('.repeat-group input').on('focus', function(e) {
if ( self.correctClassAdded( $(this) ) ) {
console.log('working');
}
console.log($(this).parent());
var group = $(this).parent().siblings().children();
var groupInputValArray = [];
var emptyCounter = 0;
for ( a = 0; a <= group.length -1; a++){
//if the input does not have a value add null
if ( group[a].value === ""){ emptyCounter++; }
groupInputValArray.push(group[a].value);
}
//if the array contains only one null then add another form group
if ( emptyCounter == 1){
//then we will go ahead and add our new group
}
});
});
}
this.appendGroup = function(){
};
this.init();
}
var newfields = new formAutoGenerator();
var formAutoGenerator=函数(){
var self=this;//$有自己的“this”
this.repeatGroup=$('.repeat group');
this.formGroupLength=this.repeatGroup.length;
this.init=函数(){
此参数为.inputFocus();
这个.correctClassAdded();
};
this.classTag='重复组';
this.correctClassAdded=函数(formGroup){
//log($(formGroup.offsetParent());
返回表单组;
};
this.inputFocus=函数(){
$(文档).ready(函数(){
$('.repeat group input')。在('focus',函数(e)上{
if(self.correctClassAdded($(this))){
console.log('working');
}
log($(this.parent());
var group=$(this.parent().sibbins().children();
var groupInputValArray=[];
var emptyCounter=0;
对于(a=0;a,它正在检索您试图获取的节点的对象
您有两种选择之一:
执行$(this.parent()[0];
以获取div父节点
或者this.parentNode;
你想混合使用vanilla JS和jQuery做什么?$(document).ready(函数(){
在事件处理程序中?认真地说?$(document).ready()
页面加载时只触发一次,这应该在全局文档范围内,而不是在另一个范围内function@PraveenKumar-混合使用vanilla JS和jQuery没有什么问题…只是不是那样。;)console.log(this.parentNode);应该能满足您的需要,但这里有更深层次的问题…ITYMthis.parentNode()
,因为它是一个DOM方法。@akiespenc不确定:$(this).parentNode();
可能会工作。您应该执行thisparentNode;
#1应该是$(this).parent()[0]
;当索引被强制返回到一个数字时,没有理由将其设为字符串。#2,要更正@Barmar提到的内容,应该是this.parentNode
,因为它不是一个函数。请随意更改答案以使用正确的语法。
var formAutoGenerator = function() {
var self = this; //$ has it's own 'this'
this.repeatGroup = $('.repeat-group');
this.formGroupLength = this.repeatGroup.length;
this.init = function(){
this.inputFocus();
this.correctClassAdded();
};
this.classTag = 'repeat-group';
this.correctClassAdded = function(formGroup){
// console.log( $(formGroup).offsetParent() );
return formGroup;
};
this.inputFocus = function() {
$(document).ready(function(){
$('.repeat-group input').on('focus', function(e) {
if ( self.correctClassAdded( $(this) ) ) {
console.log('working');
}
console.log($(this).parent());
var group = $(this).parent().siblings().children();
var groupInputValArray = [];
var emptyCounter = 0;
for ( a = 0; a <= group.length -1; a++){
//if the input does not have a value add null
if ( group[a].value === ""){ emptyCounter++; }
groupInputValArray.push(group[a].value);
}
//if the array contains only one null then add another form group
if ( emptyCounter == 1){
//then we will go ahead and add our new group
}
});
});
}
this.appendGroup = function(){
};
this.init();
}
var newfields = new formAutoGenerator();