英文:
Retrive text inside a nested span?
问题
我有以下的动态HTML,我想根据最后一个元素内的文本来改变
我尝试了一些不同的方法,但都没有给我正确的文本,所以
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var text = $(this).text()
console.log(text)
if (text == 'OK') {
$('div.fundoDiv').css("background-color", "green");
} else {
$('div.fundoDiv').css("background-color","red");
}
});
任何帮助将不胜感激。
Marcio
英文:
I have the following dynamic HTML, and I would like to change the color of the <div> based on text inside the last span element, in that case, an "OK".
<div class="data">
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">OK</span></span></span>
</div>
I have tried some different approaches, but none of them has given me the correct text, so the <div> color was not changed. In fact, the console.log(text) command has no output. Below is the jQuery I'm using for that.
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var text = $(this).text()
console.log(text)
if (text = 'OK') {
$('div.fundoDiv').css("background-color", "green");
} else {
$('div.fundoDiv').css("background-color","red");
}
});
Any help will be highly appreciated.
Marcio
答案1
得分: 1
以下是您要翻译的内容:
You forgot to trim your text to remove white spaces and compare instead of an assignment:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var text = $(this).text().trim();
console.log(text)
if (text === 'OK') {
$('div.fundoDiv').css("background-color", "green");
} else {
$('div.fundoDiv').css("background-color","red");
}
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data">
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">OK</span></span></span>
</div>
<!-- end snippet -->
英文:
You forgot to trim your text to remove white spaces and compare instead of an assignment:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var text = $(this).text().trim();
console.log(text)
if (text === 'OK') {
$('div.fundoDiv').css("background-color", "green");
} else {
$('div.fundoDiv').css("background-color","red");
}
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data">
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">OK</span></span></span>
</div>
<!-- end snippet -->
答案2
得分: 0
Use $(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").text().trim().
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var text = $(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").text().trim();
console.log("{"+text+"}");
if (text == 'OK') {
$('div.fundoDiv').css("background-color", "green");
} else {
$('div.fundoDiv').css("background-color","red");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data">
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">OKf</span>
</span>
</span>
</div>
</div>
英文:
Use $(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").text().trim()
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var text = $(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").text().trim();
console.log("{"+text+"}");
if (text == 'OK') {
$('div.fundoDiv').css("background-color", "green");
} else {
$('div.fundoDiv').css("background-color","red");
}
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data">
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">OKf</span></span></span>
</div>
<!-- end snippet -->
答案3
得分: 0
-
在
if内部,你不是在进行比较,而是在进行赋值,所以需要修正这一点。 (=是赋值操作符,==或===用于比较) -
对文本进行修剪,以便在比较时忽略任何开头/结尾的空格。
-
你需要同时使用
.closest()来获取相应的fundoDivdiv,以便在比较成功或失败后执行操作(因为你正在循环遍历fundoDiv内部的 span,而不是fundoDiv本身,所以$(this)实际上会反映为<span>)。
工作代码片段:
$(document).ready(function() {
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var text = $(this).find('span').text().trim();
if (text === 'OK') {
$(this).closest('.fundoDiv').css("background-color", "green");
} else {
$(this).closest('.fundoDiv').css("background-color", "red");
}
});
});
如果你的具有 class EmbeddedMiniatureVisualization 的 span 包含多个 span,并且你想要搜索和匹配它们中的每一个,那么可以按照以下方式进行:**
$(document).ready(function() {
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var obj = $(this);
var isTextFound = false;
obj.find('span').each(function() {
if ($(this).text().trim() == 'OK') {
isTextFound = true;
return;
}
});
if (isTextFound) {
obj.closest('.fundoDiv').css("background-color", "green");
} else {
obj.closest('.fundoDiv').css("background-color", "red");
}
});
});
希望这对你有所帮助!
英文:
-
Inside
ifYou are not comparing, you are doing an assignment so fix that. (=is assignment and==or===used for comparison) -
Trim the text so that in case any starting/trailing space is there then they will get neglected while comparison.
-
You need to use
.closest()as well to get the correspondingfundoDivdiv after the comparison becomes successful or fails. (because you are looping over the span insidefundoDivnot on thefundoDivitself so$(this)will reflect<span>actually)
Working snippet:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var text = $(this).find('span').text().trim();
if (text === 'OK') {
$(this).closest('.fundoDiv').css("background-color", "green");
} else {
$(this).closest('.fundoDiv').css("background-color", "red");
}
});
});
<!-- language: lang-css -->
.fundoDiv{
margin-bottom : 20px;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data">
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">OK</span></span>
</span>
</div>
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">Not OK</span></span>
</span>
</div>
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">OK</span></span>
</span>
</div>
</div>
<!-- end snippet -->
In case your span with the class EmbeddedMiniatureVisualization contains multiple spans, and you want to search and match each of them then do like below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$(".fundoDiv span.EmbeddedMiniatureVisualization:last-child").each(function() {
var obj = $(this);
var istextFound = false;
obj.find('span').each(function() {
if ($(this).text().trim() == 'OK') {
istextFound = true;
return;
}
});
if (istextFound) {
obj.closest('.fundoDiv').css("background-color", "green");
} else {
obj.closest('.fundoDiv').css("background-color", "red");
}
});
});
<!-- language: lang-css -->
.fundoDiv{
margin-bottom : 20px;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="data">
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">OK</span>
<span style="color: rgb(97, 100, 107); text-align: left;">Not OK</span>
</span>
</div>
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">Not OK</span>
<span style="color: rgb(97, 100, 107); text-align: left;">Not OK</span>
</span>
</div>
<div class="fundoDiv">
<section>
<header class="textoHeader">Risk</header>
</section>
<span id="b74ac226bea04e10a80e25bbf7e529a3" style="font-family:'Arial';font-size:37px;">
<span id="f95f8aba-1b60-4174-9354-8945634b6179" viewid="f95f8aba-1b60-4174-9354-8945634b6179" class="EmbeddedMiniatureVisualization" sf-busy="false" style="position: relative; margin: 0px; padding: 0px; border: 0px; overflow: hidden;">
<span style="color: rgb(97, 100, 107); text-align: left;">OK</span>
<span style="color: rgb(97, 100, 107); text-align: left;">Not OK</span>
</span>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论