gpt4 book ai didi

javascript - jQuery:附加列表不显示元素符号

转载 作者:行者123 更新时间:2023-11-28 05:08:59 25 4
gpt4 key购买 nike

我有以下 html:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<span>not so important</span>
<span title="some specific text"><img src="/img/some.gif" /></span>
<span title="more specific text">important 1</span>
<span title="more specific text">important 2</span>
<span title="more specific text">important 3</span>
<span title="more specific text"><img src="/img/some.gif" /></span>
<ul>
<li>example 1</li>
<li>example 2</li>
<li>example 3</li>
<ul>
<script>
var ul = $('body').append($('<ul>'));
$("span[title*='specific text']")
.contents()
.filter(function(){ return $(this).nodeType != 1; })
.each(function(){
ul.append($('<li>' + $(this).text() + '</li>'));
})
</script>
</body>
</html>

我想检索标题属性包含“特定文本”的“跨度”中的文本,在本例中为“重要 1”、“重要 2”和“重要 3”。然后我想像示例一样在下面列出它们。但是子弹没有出现,这是为什么呢?任何提示将不胜感激!

编辑: http://jsfiddle.net/XTqjC/3/这是执行我需要它执行的操作的代码。感谢 Glen 和 meo

最佳答案

在您的示例中,您还没有关闭 <ul>正确(缺少斜线)。这是复制粘贴错误,还是您的代码中也有?

编辑: 你需要 在你的 each() 之后有一个分号是个好习惯(见评论):

.each(function(){
ul.append('<li>' + $(this).text() + '</li>');
});

尝试以下操作:

var newUL = $('<ul/>');    //due credit to meo (see below)
$('body').append(newUL);
$("span[title*='specific text']")
.contents()
.filter(function(){ return $(this).nodeType != 1; })
.each(function(){
$("body ul").append('<li>' + $(this).text() + '</li>');
});

当然,$(this).text()不允许您放置 <img><li>秒。所以你最终会得到一个空的 <li></li>对于每个。这可以通过以下方式解决:

.each(function(){
if ($(this).text().trim() != "") {
$("body ul").append('<li>' + $(this).text() + '</li>');
}
});

关于javascript - jQuery:附加列表不显示元素符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2719800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com