Jquery 使用父div';s范围
在下面关于buton的代码中,单击parent div的输入文本将被提示。我不清楚“使用父divs作用域”是什么意思。有人能给我解释一下这个作用域吗 HTML 您可以使用直接获取电子邮件Jquery 使用父div';s范围,jquery,scope,Jquery,Scope,在下面关于buton的代码中,单击parent div的输入文本将被提示。我不清楚“使用父divs作用域”是什么意思。有人能给我解释一下这个作用域吗 HTML 您可以使用直接获取电子邮件 $('.btn').on('click', function () { var email = $(this).siblings().val(); alert(email); }); 当您使用语法$('selector1','selector2')在元素的内部找到元素时,您将在“sele
$('.btn').on('click', function () {
var email = $(this).siblings().val();
alert(email);
});
当您使用语法
$('selector1','selector2')
在元素的内部找到元素时,您将在“selector2”的范围内找到带有“selector1”的元素。从您对问题的评论中:
有人给了我这段代码,说var email=$('.email',$this.val();//这行使用parent div作为作用域
他们的术语不正确,但他们想说的是:
var email = $('.email', $this).val();
…将仅在$this
jQuery集中查找与选择器.email
匹配的元素,这些元素是div的后代,然后获取该元素的值
这条线相当于:
var email = $this.find('.email').val();
…事实上,如果您使用$(选择器,$this)
,jQuery只会反过来执行$this.find(选择器)
var $this = $(this).parents('.info');
var email = $('.email', $this).val();
电子邮件表示具有.info
类的元素中具有.email
类的元素的值(其中$this
元素是单击按钮的父元素)
希望现在一切都清楚了
您可以参考以获得更好的理解。作为旁注,使用
$(this)。最接近('.info')
您确定可以获得这样的2个元素的值$('.email',$this).val();
还是拼写错误$('.email'$this).val();
“我不清楚“使用父divs作用域”是什么意思。”该短语与您发布的内容无关,您必须询问使用该短语的人。@Arun使用parents()是否错误。我是JQueryId新手,你是说范围
或上下文
// this traverses the ancestry of that button until it finds an element
// with a class "info", and then caches that jQuery object in $this
var $this = $(this).parents('.info');
// this searches for element with class "email" inside the cached element $this
// (which is the parent as traversed above)
var email = $('.email', $this).val();
var email = $('.email', $this).val();
var email = $this.find('.email').val();
var $this = $(this).parents('.info');
var email = $('.email', $this).val();