使用jquery在div中的锚标记中添加一个类

时间:2016-09-01 14:43:34

标签: javascript jquery html class

有谁知道如何在A标签中添加一个类:

<a href="/souscription/digital-vie">Subscribe</a>

这是我的HTML:

<div class="module" id="prod-subscription">
    <div class="entity entity-bean bean-ap-bl-instruct contextual-links-region block container">
        <div class="contextual-links-wrapper contextual-links-processed">
            <a class="contextual-links-trigger" href="#">Configure</a>
            <ul class="contextual-links">
                <li class="bean- first last">
                    <a href="/block/souscription/edit?destination=node/3719"> bloc</a>
                </li>
            </ul>
        </div>    
        <div class="row">
            <div class="col-md-3">
                <h2>simple<span>in 3 </span></h2>
            </div>
            <div class="col-md-6">
                <ol>
                    <li>
                        <span>1</span><p>text</p>
                    </li>
                    <li>
                        <span>2</span><p>texte testx</p>
                    </li>
                    <li>
                        <span>3</span><p>and text</p>
                    </li>
                </ol>
            </div>
            <div class="col-md-3">
                <a href="/souscription/digital-vie">Subscribe</a>
            </div>
        </div>
    </div>
</div>

3 个答案:

答案 0 :(得分:1)

您可以使用jQuery&#39; filter function并过滤确切文字为&#39;订阅&#39;的链接。 (如果您有多个具有该确切文本的链接,这可能会导致问题)

&#13;
&#13;
$('a').filter(function() {
  return $(this).text() === 'Subscribe';
}).addClass('new-class');
&#13;
.new-class {color:red;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="/souscription/digital-vie">Subscribe</a>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你可以使用

$('a[href="/souscription/digital-vie"]').addClass('classHere');

$('div#prod-subscription a[href="/souscription/digital-vie"]').addClass('classHere');

$('a[href="/souscription/digital-vie"]:contains("Subscribe")').addClass('classHere');

$('a[href="/souscription/digital-vie"]:contains("Subscribe")').addClass('classHere');
.classHere{
  background : red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="module" id="prod-subscription">
    <div class="entity entity-bean bean-ap-bl-instruct contextual-links-region block container">
        <div class="contextual-links-wrapper contextual-links-processed">
            <a class="contextual-links-trigger" href="#">Configure</a>
            <ul class="contextual-links">
                <li class="bean- first last">
                    <a href="/block/souscription/edit?destination=node/3719"> bloc</a>
                </li>
            </ul>
        </div>    
        <div class="row">
            <div class="col-md-3">
                <h2>simple<span>in 3 </span></h2>
            </div>
            <div class="col-md-6">
                <ol>
                    <li>
                        <span>1</span><p>text</p>
                    </li>
                    <li>
                        <span>2</span><p>texte testx</p>
                    </li>
                    <li>
                        <span>3</span><p>and text</p>
                    </li>
                </ol>
            </div>
            <div class="col-md-3">
                <a href="/souscription/digital-vie">Subscribe</a>
            </div>
        </div>
    </div>
</div>

答案 2 :(得分:0)

不确定我完全理解这个问题。你只是想给锚标签一个类吗?这可以像<a class="your_class" href="your_link">Subscribe</a>

一样简单地完成