使用jquery,如何获得li元素的"inner text"?
Using jquery, how can I get this "inner text" of a li element?
我有以下 html 并且我正在尝试找出正确的选择器逻辑来读取某些部分。 注意:我无法更改 html,因为这是由插件生成的。
<li class="select2-selection__choice" title="">
<span class="select2-selection__choice__remove" role="presentation">×</span>
MS Office
</li>
<li class="select2-selection__choice" title="">
<span class="select2-selection__choice__remove" role="presentation">×</span>
Photoshop
</li>
我正在尝试读出
中的文字
<li class="select2-selection__choice">
但不包括
<span class="select2-selection__choice__remove">
所以对于上面的示例,我希望解析出以下文本:
MS Office, Photoshop
这样试试
$('.select2-selection__choice').each(function() {
var text = $(this).clone().children().remove().end().text();
console.log(text.trim());
});
片段
$('.select2-selection__choice').each(function() {
var text = $(this).clone().children().remove().end().text();
console.log(text.trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="select2-selection__choice" title="">
<span class="select2-selection__choice__remove" role="presentation">×</span>
MS Office
</li>
<li class="select2-selection__choice" title="">
<span class="select2-selection__choice__remove" role="presentation">×</span>
Photoshop
</li>
使用 .class
选择器:
$('.select2-selection__choice').each(function() {
var inneText = $(this).children().text();
});
我有以下 html 并且我正在尝试找出正确的选择器逻辑来读取某些部分。 注意:我无法更改 html,因为这是由插件生成的。
<li class="select2-selection__choice" title="">
<span class="select2-selection__choice__remove" role="presentation">×</span>
MS Office
</li>
<li class="select2-selection__choice" title="">
<span class="select2-selection__choice__remove" role="presentation">×</span>
Photoshop
</li>
我正在尝试读出
中的文字 <li class="select2-selection__choice">
但不包括
<span class="select2-selection__choice__remove">
所以对于上面的示例,我希望解析出以下文本:
MS Office, Photoshop
这样试试
$('.select2-selection__choice').each(function() {
var text = $(this).clone().children().remove().end().text();
console.log(text.trim());
});
片段
$('.select2-selection__choice').each(function() {
var text = $(this).clone().children().remove().end().text();
console.log(text.trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="select2-selection__choice" title="">
<span class="select2-selection__choice__remove" role="presentation">×</span>
MS Office
</li>
<li class="select2-selection__choice" title="">
<span class="select2-selection__choice__remove" role="presentation">×</span>
Photoshop
</li>
使用 .class
选择器:
$('.select2-selection__choice').each(function() {
var inneText = $(this).children().text();
});