2016-09-12 2 views
-2

Мне нужно изменить текст в промежутке.Как получить текст элемента в span с помощью jquery?

<span class="count_bottom"> 
    <i class="blue"> 
     <i class="fa fa-sort-asc"></i> 
     12% 
    </i> 
    From last seen 
</span> 

Мне нужно только изменить From last seen текст в соответствии с событием нажатия кнопки. Как я могу это сделать?

+2

Пожалуйста, включите код, который вы пробовали. Если вы даже ничего не пробовали, есть миллионы обучающих программ jQuery, и вы определенно должны сделать хотя бы один из них. – Sevanteri

+0

Почему бы вам не обернуть текст в какой-то другой '' и дать ему уникальный класс для доступа к нему? – vijayP

ответ

2

Фильтр из непустых текстовые узлы внутри пространства, а затем заменить новым содержанием.

$('.count_bottom') 
 
    // get all child nodes 
 
    .contents() 
 
    // filter out non-empty text node 
 
    .filter(function() { 
 
    // check node type and content 
 
    return this.nodeType === 3 && this.nodeValue.trim().length; 
 
    // replace it with new content 
 
    }).replaceWith('new text');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span class="count_bottom"><i class="blue"><i class="fa fa-sort-asc"></i>12% </i> From last seen</span>


С чистого JavaScript

// get the `i` tag inside `span` 
 
document.querySelector('.count_bottom > i') 
 
    // get adjacent text node which is immediately after the element 
 
    .nextSibling 
 
    // update text content of the text node 
 
    .textContent = 'new text';
<span class="count_bottom"><i class="blue"><i class="fa fa-sort-asc"></i>12% </i> From last seen</span>

0

Вы можете выбрать первый элемент i в span и использовать свойство nextSibling для получения следующего текста его сестры.

$("button").click(function(){ 
 
    $(".count_bottom > i")[0].nextSibling.nodeValue = "New value"; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button>Change text</button> 
 
<span class="count_bottom"> 
 
    <i class="blue"> 
 
     <i class="fa fa-sort-asc"></i> 
 
     12% 
 
    </i> 
 
    From last seen 
 
</span>

+0

Ответ правильный. Удалите downvote. – Mohammad

Смежные вопросы