我想在 var roll_time = $('#banner')[0].childNodes[0].textContent;
时运行一个函数是 == "Rolling in 25.00..."
我尝试使用此代码执行此操作,但没有得到输出。 <span>
中的文字每 10 毫秒改变一次。
setInterval(test, 10)
function test(roll_time) {
var roll_time = $('#banner')[0].childNodes[0].textContent;
if (roll_time == "Rolling in 25.20...") {
console.log(success) }
}
<span id="banner">Rolling in 25.20...</span>
最佳答案
那是因为您正在尝试访问在加载期间填充值的 roll_time 。
$('#banner')[0].childNodes[0].textContent 是一个字符串,而不是引用,因此如果您需要稍后访问该字符串,则需要显式读取该值来自 DOM。
setInterval(test, 10)
function test() {
var roll_time = $('#banner')[0].childNodes[0].textContent;
if (roll_time == "Rolling in 25.20...") {
console.log("success");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="banner">Rolling in 25.20...</span>
关于javascript - 如果文本发生变化,如何使用带跨度的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40597079/