检查span属性包含Jquery中的文本

时间:2017-12-27 02:39:27

标签: jquery

只是徘徊,是否可以在Jquery中检查span属性是否包含文本?

实施例: 我有以下HTML代码:

<div id="test">
    <div class="user-name">
     <span class="user_title">Mrs Support</span>
     <span class="org_name" org-full-name="Test">Moon Company</span>
     <span><a class="mutual">3 mutual user</a></span></div>
</div>

我想找出属性org-full-name值包含&#34; es&#34;

我尝试了以下代码,但它无效

$("#test").find(".user-name:icontains('es')")

3 个答案:

答案 0 :(得分:1)

这是一种替代方法,但您可以使用.indexOf。它将根据文本返回true或False

&#13;
&#13;
console.log($("#test span[org-full-name=Test]").text().indexOf("es")>=0)


//alternate way to have the text based approach
console.log($("#test span[org-full-name]").text().indexOf("es")>=0)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">
    <div class="user-name">
     <span class="user_title">Mrs Support</span>
     <span class="org_name" org-full-name="Test">Test</span>
     <span><a class="mutual">3 mutual user</a></span></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用filter():contains选择器仅适用于文本,不适用于属性值

$("#test .user-name [org-full-name]").filter(function(){
   return $(this).attr('org-full-name').toLowerCase().indexOf('es')>-1
}).css('color','red')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">
  <div class="user-name">
    <span class="user_title">Mrs Support</span>
    <span class="org_name" org-full-name="Test">Moon Company</span>
    <span><a class="mutual">3 mutual user</a></span>
  </div>
    <div class="user-name">
    <span class="user_title">Uncle Support</span>
    <span class="org_name" org-full-name="Foo">Howl at Moon</span>
    <span><a class="mutual">9 mutual user</a></span>
  </div>
      <div class="user-name">
    <span class="user_title">Support Junior</span>
    <span class="org_name" org-full-name="Especially">Unemployed</span>
    <span><a class="mutual">9 mutual user</a></span>
  </div>
</div>

答案 2 :(得分:0)

你可以这样做:

<?php

/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\Question[]|\Cake\Collection\CollectionInterface 
$questions
*/
use Cake\ORM\TableRegistry;
?>
 <nav class="large-3 medium-4 columns" id="actions-sidebar">
  <ul class="side-nav">
    <li class="heading"><?= __('Actions') ?></li>
    <li><?= $this->Html->link(__('Courses'), ['controller' => 'Courses', 
  'action' => 'index']) ?></li>
</ul>
</nav>
</nav>
 <div class="questions form large-9 medium-8 columns content">
 <fieldset>
    <legend><?= __('Add Question') ?></legend>
    <table cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th scope="col"><?= $this->Paginator->sort('checked') ?></th>
            <th scope="col"><?= $this->Paginator->sort('id') ?></th>
            <th scope="col"><?= $this->Paginator->sort('question') ?></th>
            <th scope="col"><?= $this->Paginator->sort('marks') ?></th>
        </tr>
    </thead>
    <tbody>


    <?php echo $this->Form->create($question) ?>   
        <?php foreach ($questions as $question): ?>
        <tr>
            <td><?= $this->Form->checkbox('id[].', array('value' => 
    $question['id'], 'name' => 'Question[id][]', 'checked'=>false))?></td>
            <td><?= $this->Number->format($question->id) ?></td>
            <td><?= h($question->question) ?></td>
            <td><?= $this->Number->format($question->marks) ?></td>
        </tr>
        <?php endforeach; ?>                 
    </tbody>
    </table>
    <input type="submit" value="Save">        
    </form>
    <div class="paginator">
    <ul class="pagination">

        <?= $this->Paginator->first('<< ' . __('first')) ?>
        <?= $this->Paginator->prev('< ' . __('previous')) ?>
        <?= $this->Paginator->numbers() ?>
        <?= $this->Paginator->next(__('next') . ' >') ?>
        <?= $this->Paginator->last(__('last') . ' >>') ?>
    </ul>
    <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
     <?php //$this->Form->button(__('Submit'), ['action' => 'existingQuestion'])
         $this->Form->submit('Save'); 
        // $this->Form->end() ?>
</div>
</fieldset>

如果属性值中包含“es”,则返回true,否则返回false。