微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

嵌套的jQuery选择器

反正有没有嵌套的jQuery选择器.

例如:

如果该页面上还有一个ID =“LeadEditForm_Title”,那么请执行以下操作…

jQuery("[id='A0.R0.Main Phone Number']").live('mousedown',function(e) {
    var container = $(this).width();
    var width_offset = -50;
    var top_offset = 25;
    var width = (container + width_offset).toString();
    jQuery(".mywidget").appendTo(document.body).css('width',width + 'px');
    jQuery(".mywidget").appendTo(document.body).css('position','absolute');
    jQuery(".mywidget").appendTo(document.body).css('top',($(this).offset().top+top_offset).toString() + 'px');
    jQuery(".mywidget").appendTo(document.body).css('left',Math.round($(this).offset().left) + 'px');
});

题:

我怎么做一个if jQuery(“.LeadEditForm_Title”)然后做上面的???基本上是一个嵌套的jQuery调用…我见过他们有的例子:

jQuery(function(){ // my code goes here });

但我希望它依赖于ID“.LeadEditForm_Title”.

解决方法

要嵌套选择器,请使用find:
jQuery('#mySelectorId').find('.mySelectorClass')

这与执行此操作相同:

jQuery('#mySelectorId .mySelectorClass')

一定要在两者之间留一个空间.没有空间,您将选择具有该ID和该类的元素.

我还会注意到你的代码可能没有按照你的想法做到:

jQuery("[id='A0.R0.Main Phone Number']").live('mousedown',Math.round($(this).offset().left) + 'px');
});

最后4个jQuery(“.mywidget”)调用每次都将小部件添加到正文中.您真的只想添加一次并更改每种样式的css:

jQuery("[id='A0.R0.Main Phone Number']").live('mousedown',width + 'px').css('position','absolute').css('top',($(this).offset().top+top_offset).toString() + 'px').css('left',Math.round($(this).offset().left) + 'px');
});

哪个也可以减少到一个css调用

jQuery("[id='A0.R0.Main Phone Number']").live('mousedown',function(e) {
    var container = $(this).width();
    var width_offset = -50;
    var top_offset = 25;
    var width = (container + width_offset).toString();
    jQuery(".mywidget").appendTo(document.body).css({
        width: width + 'px',position: 'absolute',top: ($(this).offset().top+top_offset).toString() + 'px',left: Math.round($(this).offset().left) + 'px';
    });
});

注意,根据HTML规范,除此之外,你的id不应该有空格.并且,如果您有一个有效的ID,您可以选择它:

jQuery("#A0.R0.Main_Phone_Number")

原文地址:https://www.jb51.cc/jquery/177633.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐