如果我有这样的HTML:
<li id="listItem">
This is some text
<span id="firstSpan">First span text</span>
<span id="secondSpan">Second span text</span>
</li>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用.text()只检索字符串"这是一些文本",但如果我要说$('#list-item').text(),我得到"这是一些textFirst span text secondSecond span text".
有没有办法获得(并可能通过类似的东西.text(""))只是标签内的自由文本,而不是其子标签中的文本?
HTML不是我写的,所以这就是我必须要处理的.我知道在编写html时将文本包装在标签中会很简单,但同样,html是预编写的.
// if the box is outside the window, move it to the end
function checkEdge() {
var windowsLeftEdge = $('#window').position().left;
$('.box').each( function(i, box) {
// right edge of the sliding box
var boxRightEdge = $(box).position().left + $(box).width();
// position of last box + width + 10px
var newPosition = getNewPosition();
if ( parseFloat(boxRightEdge) < parseFloat(windowsLeftEdge) ) {
$(box).css('left', newPosition);
$(box).remove().appendTo('#window');
first = $('.box:first').attr('class');
}
});
}? //Uncaught SyntaxError: Unexpected token ILLEGAL Occurs Here
// arrange the boxes to be aligned …Run Code Online (Sandbox Code Playgroud) 说我有这个:
<div class='myDiv'>
<p>hello</p>
hello
<p>Hello</p>
</div>
Run Code Online (Sandbox Code Playgroud)
如何使用jQuery抓取两个P标签之间的文本问候?
我想清除“正常文本”,但没有字形。
\n\n重要的代码部分及其结果:
\n\n$("#btn_test").html()会给我:
"<span class="glyphicon glyphicon-floppy-disk"\naria-hidden="true"></span> Speichern & schlie\xc3\x9fen"
$("#btn_test").text()会给我:
" Speichern & schlie\xc3\x9fen"
我只想删除文本 " " Speichern & schlie\xc3\x9fen",不带.replace(...)-function (因为文本是动态的)。
我尝试过$("#btn_test").text(""),但这也会清除我的跨度元素。
谢谢。
\n\n更新(\'可能重复\'): \n是的,这是与这里相同的问题,但不是克里斯·戴维斯的答案,它解决了我的问题。
\n