【问题标题】:Using jQuery to use one class when multiple classes are returned返回多个类时使用jQuery使用一个类
【发布时间】:2013-08-30 10:40:00
【问题描述】:

我正在存储一系列这样的类:

var sectionParentID = $(this).parent('section').attr('class');

父级有多个类。例如,上述变量可能返回“section one titleAdded”或“section two titleAdded”或“section three titleAdded

我关心的类是数字:一、二、三等。有了这些数字,我需要定位另一个具有相同类的 div(一、二、三等)

var sectionParentSelector =  $(".product_totals").find(" ." + sectionParentID);

当我像这样记录选择器时:console.log(sectionParentSelector) 它返回以下不正确的选择器:

.product_totals  .section two titleAdded

我需要它返回.product_totals .two

我可以做些什么来隔离这里需要的类?

【问题讨论】:

标签: jquery jquery-selectors selector


【解决方案1】:

试试

var sectionParentID = $(this).parent('section').attr('class');
sectionParentID  = $.trim(sectionParentID.replace(/(section)|(titleAdded)/g, ''))
var sectionParentSelector =  $(".product_totals").find(" ." + sectionParentID);

【讨论】:

    猜你喜欢
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多