1

次のマークアップがあります。URLの一部を元にアンカータグを合わせたい。

実際、私は以下のようにマークアップしました。そして、ソートフィールド名を知っています。フィールド名に基づいてアンカーを選択したい。sort:resource_locatorこれに基づいて、最初のアンカー タグを選択し、小さなup/down矢印を追加する方向に従ってクラスを指定します。

<th><a href="/sme_leads/index/page:1/sort:resource_locator/direction:asc">Resource Locator</a></th>
<th><a href="/sme_leads/index/page:1/sort:business_name/direction:asc">Business Name</a></th>

情報に基づいてアンカータグを選択する方法を知りたいですsort:resource_locator

私は$ and ^それぞれ終わりと始まりを知っています。

4

2 に答える 2

3

Attribute Contains Selector[name*="value"]を使用できます。

$('a[href*="sort:resource_locator"]')

デモ

于 2012-09-05T17:14:58.903 に答える
2

aに が含まれる要素を選択するsort:にはhref:

$('a').filter(
    function(){
        return this.href.indexOf('sort:') !== -1;
    });

direction:文字列 (ascまたは)に続く文字列に基づいてクラスを割り当てるにはdesc:

$('a').filter(
    function(){
        return this.href.indexOf('sort:') !== -1;
    }).addClass(
        function(){
            var href = this.href,
                matches = href.match(/direction\:([a-z]+)/);
            return matches[1];
        });​

JS フィドルのデモ

于 2012-09-05T17:16:29.733 に答える