【问题标题】:Get number of class by part name按零件名称获取班级编号
【发布时间】:2013-01-25 18:08:34
【问题描述】:

我有以下元素:

<ul id="towns">
    <li class="clearfix odd 516_499_132_0_0 town132">...</li>
</ul>

我需要为一个参数提取类“town132”,但数字 - 例如 132 - 是可变的。

如何提取?

$('#towns').children('li').each(function() {
    parameter = $(this). // ???
});

【问题讨论】:

  • 您是否可以控制呈现的 HTML?如果可能,我建议更改它,以便将此值存储在 data- 属性中(例如 data-town="132")。

标签: javascript jquery regex string replace


【解决方案1】:

如果你知道课程会一直在那里:

parameter = this.className.match(/town(\d+)/)[1];

否则:

var m = this.className.match(/town(\d+)/);
if (m) {
    parameter = m[1];
}

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多